# ERROR: Code doesn't return items properly

**URL:** <https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419>\
**Category:** Questions\
**Tags:** node, http-request\
**Created:** [December 3, 2022, 4:14am UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419 "2022-12-03T04:14:54Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Msaustral](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Msaustral](https://community.n8n.io/u/Msaustral)\
**Post date:** [December 3, 2022, 4:14am UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/1 "2022-12-03T04:14:54Z")

</div>

Hi i have this JSON

```auto
[
  {
    "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

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

```

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

if i do

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![jan](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jan/32/9_2.png) [@jan](https://community.n8n.io/u/jan)\
**Post date:** [December 3, 2022, 11:38am UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/2 "2022-12-03T11:38:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Msaustral](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Msaustral](https://community.n8n.io/u/Msaustral)\
**Post date:** [December 3, 2022, 2:10pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/3 "2022-12-03T14:10:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![yukyo](https://avatars.discourse-cdn.com/v4/letter/y/e480ec/32.png) [@yukyo](https://community.n8n.io/u/yukyo)\
**Post date:** [December 3, 2022, 7:48pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/4 "2022-12-03T19:48:30Z")

</div>

You could try with:

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

```

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

---

<div class="post-metadata">

**Author:** ![Msaustral](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Msaustral](https://community.n8n.io/u/Msaustral)\
**Post date:** [December 5, 2022, 1:06pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/5 "2022-12-05T13:06:05Z")

</div>

thank you Yukyo

---

<div class="post-metadata">

**Author:** ![Msaustral](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Msaustral](https://community.n8n.io/u/Msaustral)\
**Post date:** [December 5, 2022, 2:20pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/6 "2022-12-05T14:20:19Z")

</div>

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

 ![n8n](https://europe1.discourse-cdn.com/n8n/original/3X/0/e/0e05398a638b40eee05c2c195012b1f9ad19ff94.png)

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [December 6, 2022, 12:02pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/7 "2022-12-06T12:02:28Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![Msaustral](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Msaustral](https://community.n8n.io/u/Msaustral)\
**Post date:** [December 6, 2022, 12:43pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/8 "2022-12-06T12:43:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Msaustral](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Msaustral](https://community.n8n.io/u/Msaustral)\
**Post date:** [December 7, 2022, 4:22pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/9 "2022-12-07T16:22:22Z")

</div>

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 ☹ ?

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [December 8, 2022, 3:02pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/10 "2022-12-08T15:02:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Msaustral](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Msaustral](https://community.n8n.io/u/Msaustral)\
**Post date:** [December 9, 2022, 3:04pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/11 "2022-12-09T15:04:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Msaustral](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Msaustral](https://community.n8n.io/u/Msaustral)\
**Post date:** [December 12, 2022, 5:56pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/12 "2022-12-12T17:56:19Z")

</div>

Hi, any idea on how to solve this?

---

<div class="post-metadata">

**Author:** ![jan](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jan/32/9_2.png) [@jan](https://community.n8n.io/u/jan)\
**Post date:** [December 13, 2022, 12:22am UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/13 "2022-12-13T00:22:44Z")

</div>

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:

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

```

for the second parameter “id\_prestashop”:

```javascript
{{ $input.item.json.orders[0].id }}

```

---

<div class="post-metadata">

**Author:** ![Msaustral](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Msaustral](https://community.n8n.io/u/Msaustral)\
**Post date:** [December 13, 2022, 3:31pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/14 "2022-12-13T15:31:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jan](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jan/32/9_2.png) [@jan](https://community.n8n.io/u/jan)\
**Post date:** [December 13, 2022, 7:25pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/15 "2022-12-13T19:25:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [March 13, 2023, 7:25pm UTC](https://community.n8n.io/t/error-code-doesnt-return-items-properly/20419/16 "2023-03-13T19:25:33Z")

</div>

Closed on March 13, 2023, 7:25pm UTC

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