# Use of NodeJs Code witin Function Node

**URL:** <https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479>\
**Category:** Questions\
**Tags:** node\
**Created:** [July 5, 2022, 2:42pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479 "2022-07-05T14:42:00Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan\_Pablo\_Sanchez](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juan\_Pablo\_Sanchez](https://community.n8n.io/u/Juan_Pablo_Sanchez)\
**Post date:** [July 5, 2022, 2:42pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/1 "2022-07-05T14:42:00Z")

</div>

## Describe the issue/error/question

Hi, i have a code from postam working and cpy paste the NodeJs Request to a new node ( function).  
don´t know why the result is with MyNewField = 1 and the response is not there

## What is the error message (if any)?

## Please share the workflow

```auto
(Select the nodes and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow respectively)

```

## Share the output returned by the last node

## Information on your n8n setup

- **n8n version:**
- **Database you’re using (default: SQLite):**
- **Running n8n with the execution process [own(default), main]:**
- **Running n8n via [Docker, npm, n8n.cloud, desktop app]:**

---

<div class="post-metadata">

**Author:** ![BramKn](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/bramkn/32/11894_2.png) [@BramKn](https://community.n8n.io/u/BramKn)\
**Post date:** [July 5, 2022, 2:52pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/2 "2022-07-05T14:52:22Z")

</div>

Hi @Juan_Pablo_Sanchez

The "MyNewField = 1 " is the default value when creating a function node.  
You probably didn’t set the result to a value in the item.  
Could you share the code so we can have a look and help you from there?

---

<div class="post-metadata">

**Author:** ![Juan\_Pablo\_Sanchez](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juan\_Pablo\_Sanchez](https://community.n8n.io/u/Juan_Pablo_Sanchez)\
**Post date:** [July 5, 2022, 3:30pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/3 "2022-07-05T15:30:41Z")

</div>

// Code here will run only once, no matter how many input items there are.  
// More info and help: [Function - n8n Documentation](https://docs.n8n.io/nodes/n8n-nodes-base.function)  
// Tip: You can use luxon for dates and $jmespath for querying JSON structures

var request = require(‘request’);  
var options = {  
‘method’: ‘POST’,  
‘url’: ‘urloftheservices’,  
‘headers’: {  
‘Content-Type’: ‘application/json’,  
‘Authorization’: ‘Basic apikeyoftheservices’  
},  
body: JSON.stringify({  
“url”: “[www.ibm.com](http://www.ibm.com)”,  
“features”: {  
“categories”: {  
“limit”: 3  
}  
}  
})

};  
request(options, function (error, response) {  
if (error) throw new Error(error);  
console.log(response.body);  
});

// Loop over inputs and add a new field called ‘myNewField’ to the JSON of each one  
for (item of items) {  
item.json.myNewField = 1;  
}

// You can write logs to the browser console  
console.log(‘Done!’);

return items;

---

<div class="post-metadata">

**Author:** ![Juan\_Pablo\_Sanchez](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juan\_Pablo\_Sanchez](https://community.n8n.io/u/Juan_Pablo_Sanchez)\
**Post date:** [July 5, 2022, 3:31pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/4 "2022-07-05T15:31:59Z")

</div>

Sorry this is the right one i just substitute url and apikey in order to share it.

// Code here will run only once, no matter how many input items there are.  
// More info and help: [Function - n8n Documentation](https://docs.n8n.io/nodes/n8n-nodes-base.function)  
// Tip: You can use luxon for dates and $jmespath for querying JSON structures

var request = require(‘request’);  
var options = {  
‘method’: ‘POST’,  
‘url’: ‘urloftheservices’,  
‘headers’: {  
‘Content-Type’: ‘application/json’,  
‘Authorization’: ‘Basic passoftheservices’  
},  
body: JSON.stringify({  
“url”: “[www.ibm.com](http://www.ibm.com)”,  
“features”: {  
“categories”: {  
“limit”: 3  
}  
}  
})

};  
request(options, function (error, response) {  
if (error) throw new Error(error);  
console.log(response.body);  
});

// Loop over inputs and add a new field called ‘myNewField’ to the JSON of each one  
for (item of items) {  
item.json.myNewField = 1;  
}

// You can write logs to the browser console  
console.log(‘Done!’);

return items;

---

<div class="post-metadata">

**Author:** ![BramKn](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/bramkn/32/11894_2.png) [@BramKn](https://community.n8n.io/u/BramKn)\
**Post date:** [July 5, 2022, 4:58pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/5 "2022-07-05T16:58:16Z")

</div>

You’ll need to set the response body to a field in the item.json  
You only log it now, but this doesn’t put it as the output for the node.

---

<div class="post-metadata">

**Author:** ![Juan\_Pablo\_Sanchez](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juan\_Pablo\_Sanchez](https://community.n8n.io/u/Juan_Pablo_Sanchez)\
**Post date:** [July 5, 2022, 5:44pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/6 "2022-07-05T17:44:06Z")

</div>

Thanks  
So like,  
item.json.response=value

?¿? 😉 thanks again !

---

<div class="post-metadata">

**Author:** ![BramKn](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/bramkn/32/11894_2.png) [@BramKn](https://community.n8n.io/u/BramKn)\
**Post date:** [July 5, 2022, 5:47pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/7 "2022-07-05T17:47:59Z")

</div>

Not sure, I’ve not used a function node like this.  
With the function node you need to do it sorta like that yes. The value will then be in the response field.

Ps. Why don’t you use the HTTP node for this?

---

<div class="post-metadata">

**Author:** ![Juan\_Pablo\_Sanchez](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juan\_Pablo\_Sanchez](https://community.n8n.io/u/Juan_Pablo_Sanchez)\
**Post date:** [July 5, 2022, 6:15pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/8 "2022-07-05T18:15:22Z")

</div>

I can´t figure out how to make it work with the IBM Watson API.

---

<div class="post-metadata">

**Author:** ![BramKn](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/bramkn/32/11894_2.png) [@BramKn](https://community.n8n.io/u/BramKn)\
**Post date:** [July 5, 2022, 6:24pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/9 "2022-07-05T18:24:45Z")

</div>

do you have any documentation on the API call you want to make? maybe we can help you with that?  
Can you give an example of using the HTTP node and what goes wrong?

---

<div class="post-metadata">

**Author:** ![Juan\_Pablo\_Sanchez](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juan\_Pablo\_Sanchez](https://community.n8n.io/u/Juan_Pablo_Sanchez)\
**Post date:** [July 5, 2022, 6:38pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/10 "2022-07-05T18:38:19Z")

</div>

yes! here it is

> **[Natural Language Understanding - IBM Cloud API Docs](https://cloud.ibm.com/apidocs/natural-language-understanding)**
>
> Analyze various features of text content at scale. Provide text, raw HTML, or a public URL and IBM Watson Natural Language Understanding will give you results for the features you request. The service cleans HTML content before analysis by default,...

i´m calling the Categories  
thanks!!

---

<div class="post-metadata">

**Author:** ![BramKn](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/bramkn/32/11894_2.png) [@BramKn](https://community.n8n.io/u/BramKn)\
**Post date:** [July 5, 2022, 6:56pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/11 "2022-07-05T18:56:12Z")

</div>

Doesn’t seem to difficult.  
The Authentication is with basic user authentication.  
So username: apikey  
Password:   
You’ll need to post a Body object in JSON format.

Please try the HTTP object with the settings provided and the body you want to post.  
If you need help with that post what you did and provide any errors that may be returned.

---

<div class="post-metadata">

**Author:** ![Juan\_Pablo\_Sanchez](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@Juan\_Pablo\_Sanchez](https://community.n8n.io/u/Juan_Pablo_Sanchez)\
**Post date:** [July 12, 2022, 5:33pm UTC](https://community.n8n.io/t/use-of-nodejs-code-witin-function-node/15479/12 "2022-07-12T17:33:39Z")

</div>

sorry i didnt´comeback to you it works, thanks
