ERROR: Code doesn't return items properly

Hi i have this JSON

[
  {
    "access_token":"xxxxxxxxxxxxxxx",
    "token_type":"bearer",
    "expires_in":7200,
    "scope":"xxxxxxxxxxxxx",
    "created_at":1670038281
  }
]

i need to get the access_token

i am trying with this code

var dat = items[0].json.body.access_token;
return dat;

but i get ERROR: Code doesn't return items properly

if i do

var dat = items[0].json.body;
return dat;

its returns the full json

we need to convert this value to string to use it on a http request node

we try to add the Authorization field as a header expression doing this:

Bearer {{ $node["name"].json["body"]["access_token"] }}

but we get credential error, if we place the same value as fixed it works

Information on your n8n setup

  • **n8n version: lastes
  • **Database you’re using default: SQLite
  • **Running n8n with the execution process own(default)
  • **Running n8n via Docker

Welcome to the community @Msaustral !

The problem is that you have to at least return an object. So something like { key: 'value' }

Anyway, in this case no Code-Node is required. You can simply access it directly on the HTTP Request node. So what you have written at the total end should work. It is already a string, and even if it would not be one, it would get converted to one automatically.

What you can do to debug this issue is to open the browser console and execute the HTTP Request node. You will then see the exact call that got made in it.

Hi Jan thank you, can you help me with an example retuning a key? I did return a variable and got the same error

About the request I will try the debug console, where can I open it?

You could try with:

var dat = items[0].json.body;
return {"token": dat};

It should return an object with a “token” key and your token as the value.

1 Like

thank you Yukyo

Hi Jan, the n8n http request is not passing the value, any idea?

Hi @yukyo, this looks like a problem with the URL you are sending data to. Could you double-check this field and if you’re still having trouble perhaps share your workflow (you can simply select the relevant nodes on your canvas, then press Ctrl+C to copy them, then paste them here on the forum)?

Hi everyone, we solve it by add it as a simple authentication, but it does not work if you add it as an expression manually by a header value

Hi, sorry to bother

one last question

i have a loop, the issue is that after the loop we have another http request losing the data from previos node

to access previos nodes we use the node name but at the time the code runs, the data of previos node has change and allays brings the data from last run

with data from nodes after the las http request we did this:

var data = [];
for(let i=0; i<items.length; i++){
data.push({json:
{
prestashop:{
centry_order:{
“id_centry”: items[i].json[“id_centry”],
“id_prestashop”: items[i].json[“id_ps”],
}}
}
});
}
return data;

and i works, but if we need data from previos node is not, we try:

var data = [];
var date_add = new Date().toISOString();
for(let i=0; i<items.length; i++){
data.push({json:
{
prestashop:{
centry_logs:{
“mensaje”: “PEDIDO HOMOLOGADO”,
“error”: “PEDIDO HOMOLOGADO”,
“id_critico”: 4,
“date_add”: date_add,
“id_centry”: $node[“Datos_pedido”][i].json[“id_centry”],
“id_prestashop”: $node[“Datos_pedido”][i].json[“id_ps”],
}}
}
});
}
return data;

but we get ERROR: Cannot read properties of undefined (reading ‘json’)

Can you help me please :frowning: ?

Hi @Msaustral, I don’t think I fully understand which problem exactly you’re having :slightly_frowning_face:. Can you share a workflow using which the problem can be reproduced?

Hi, we have made an small video to show the issue that we have, thank you in advance for the help

Hi, any idea on how to solve this?

First is important to understand that you do not simply have two items, you have two different runs with one item each. If you would have one run (so each node executes just once) with two items, what you build would work. But because the nodes execute twice and $node returns always the data of the last run, do you experience this issue.

So now to the solution:

  1. You can simplify it generally by using a Set-Node instead of a Function/Code-Node. On it activate the option “Keep Only Set”. That changes everything already from multiple lines of JavaScript code to two simple expressions
  2. Instead of using the old $node["node_name"] syntax, use to the new $("node_name") one instead

The expression for the first parameter “id_centry” would then look like this:

{{ $("IF").item.json.body[0]._id }}

for the second parameter “id_prestashop”:

{{ $input.item.json.orders[0].id }}
2 Likes

Hi Jan, excellent, great, as always, thank you for your help

2 Likes

Glad to hear that it solved your issue! Have fun!

1 Like

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