Code node show Unexpected token 'return' [line 2]

it show “Unexpected token ‘return’ [line 2]”. i think different node input data to code node. Then, convert same name object to output data to AI Agent to process.

Information on your n8n setup

  • **n8n version: 1.92.2
  • **Database (default: SQLite): no
  • **n8n EXECUTIONS_PROCESS setting (default: own, main): own
  • **Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • **Operating system: windows 10

This doesnt look like valid code. Please share your workflow in a code block

This is my code block. Thank you. How to debug in n8n code node? it can not use the console.log.

$json.body.events[0].message.text != “undefined” ?
{
message_id: $json.body.events[0].message.id,
message: $json.body.events[0].message.text
} :
{
message_id: $(‘HTTP Request1’).first().json.body.events[0].message.id,
message: $input.first().json.candidates[0].content.parts[0].text
}

Incorrect parentheses: You have a closing parenthesis ) right after !== "undefined" that doesn’t match. Remove it.
Misplaced return: In JavaScript, return must be used inside a function, and it can’t be wrapped in parentheses (). Remove these parentheses from around return { json: {...} }
Comma error in message_id: There’s an extra comma after message_id: in the else part.

2 Likes

Thank you very much.

Dont use the ternary operator when returning data. Below is the correct way of writing this code:

if ($json.body.events[0].message.text !== undefined) {
  return {
    message_id: $json.body.events[0].message.id,
    message: $json.body.events[0].message.text
  }
} else {  
  return {
    message_id: $('HTTP Request1').first().json.body.events[0].message.id,
    message: $input.first().json.candidates[0].content.parts[0].text
  }
}

PS: You dont have to explicitly set the {json: {}} object. n8n will internally inject it.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.