Can't return anything or set this.exportedData from nodejs code steps

return { todos: getTodos() }
Result:
SyntaxError: 'return' outside of function. (55:0)

this.exportedData = { todos: getTodos() }
Result:
Cannot set property 'exportedData' of undefined

Full code:

import fs from "fs"

const getFiles = function getFiles(){ return fs.readdirSync("/tmp") }
const files = getFiles()
const rawNoteText = (await fs.promises.readFile(`/tmp/${files[files.length-1]}`)).toString()

function getTodoStartIndices()
{
	let match;
	const matchIndices = [],
		todoStartRegex = / ] \#task /g;

	while ( match = todoStartRegex.exec( rawNoteText ) )
	{
		matchIndices.push( match.index+match[0].length );
	}

	return matchIndices;
}
function getTodoEndIndices()
{
	const
		startIndices = getTodoStartIndices(),
		endIndices = [],
		todoEndRegex = /\n/g;

	for ( const currentStartIndex of startIndices )
	{
		const match = rawNoteText.slice(currentStartIndex)
														 .search(todoEndRegex);

		endIndices.push( match + currentStartIndex );
	}

	return endIndices/* .slice( 1 ) */;
}
function getTodos()
{
	let output = {};

	for ( let index = 0; index < getTodoStartIndices().length; index++ )
	{
		const testIndexStart = getTodoStartIndices()[index],
			testIndexEnd = getTodoEndIndices()[index],
			todo       = rawNoteText
				.slice( testIndexStart, testIndexEnd );

		output[index] = todo;
	}

	output = Object.values( output );
	return output;
}

return { todos: getTodos() }

Hello @lippiece,

First off, welcome to the Pipedream community. Happy to have you!

To return data to the next steps and solve the issue SyntaxError: 'return' outside of function. (55:0), you need to put the return statement to the run() prop in the default export function, for example:

// To use previous step data, pass the `steps` object to the run() function
export default defineComponent({
  async run({ steps, $ }) {
    // Return data to use it in future steps
    return { todos: getTodos() }
  },
})

Hello @vunguyenhung and thank you for answering.

I have tried this and get Error: EISDIR: illegal operation on a directory, read on execution.

Hello @lippiece, I believe the error Error: EISDIR: illegal operation on a directory is not really related to Pipedream platform. For me to help further, could you explain the logic behind your code, as well as the sample input and expected output?

It takes a string as input (my Obsidian note with tasks), then it finds specific start and endpoints with regex and returns an array of strings (current tasks) each consisting of everything between a start and an endpoint.

Input:

blah blah blah
- [x] Wash dishes
- [] Find the vault chip
- [] Kill boss
blah blah

Output:
["Find the vault chip", "Kill boss"]

@lippiece thanks for the information. I see that your code refer to the Workflow tmp dir to get the original notes, would you mind explaining how you upload your Obsidian notes with task to the workflow tmp dir?

Certainly. For convenience, this is a single note “Tasks Dashboard.md” which gets downloaded from Google Drive as “tasks.md”.

I played with the code a bit and fixed it with adding

export default defineComponent({
	async run({ steps, $ }) {
   		const rawNoteText = steps.code_1.$return_value
		return getTodos(rawNoteText)
 	}
})

code block.

Before this, I added a nodejs block which reads and returns the downloaded file.

I still have no idea why do we need this ...defineComponent({ async run... part. If anyone can explain, that would be very much appreciated.

Hi @lippiece

The defineComponent wrapper is necessary because it provides the hooks for the Pipedream system to run your code appropriately.

This and the rest of the Component properties are explained in this short video:

Definitely encourage you to watch that to understand the whole picture.

1 Like

Thanks, this is useful.

Can you answer two more questions please? I don’t know if I should make topics for them.

  1. Why does this site look exactly like community.n8n.io?
  2. How do I debug the code I write in the workflow blocks?

Thank you.

1 Like

I had the same question. It is possible that both use same product for managing community forum. Even then, there are so many similarities between n8n and pipedream.

Overall I liked pipedream better. Though would not say to no graphical workflow visualiser in n8n.

With respect to your other question on debugging. You can use print or console.log statements to debug. At leas thats what I do :slight_smile:

2 Likes

Great questions.

  1. Why does this site look exactly like community.n8n.io?

Probably because we both use Discourse as our community platform. It’s open source software, which we’re very much into :slight_smile:

  1. How do I debug the code I write in the workflow blocks?

You can use console.log() statements in Node.js code steps and print() statements in Python steps.

Those statements will appear in the Logs section in your code step. They’re very useful for watching the state of variables as they change through your code step’s flow.

1 Like

Thanks for the answers, @pierce and @voldemort.

Didn’t mean to accuse of anything in the first question. I’m just taking my first steps into programming and didn’t know about the Discourse.

If this is appropriate, it would be a great pleasure to have code editors integration or at least formatting support (please tell if I missed it). Often I found myself just logging a sample of data my workflow is working with and copying it to the editor I’m using, writing the code there, then copying it back into Pipedream. The interface here is indeed supportive of it and the copy buttons near the logs are very handy, although lack of formatting options leaves the sense of something unfinished.

Hi @lippiece no offense taken! Good eye.

Yes, we’re working on the ability to serialize workflows into code, so that way you can sync Pipedream workflows with Github and edit in your own IDE of choice.

That will be a huge announcement when it’s ready so stay tuned!

2 Likes