Use of NodeJs Code witin Function Node

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

(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]:

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?

1 Like

// Code here will run only once, no matter how many input items there are.
// More info and help: Function - n8n Documentation
// 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”,
“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;

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
// 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”,
“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;

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.

Thanks
So like,
item.json.response=value

?¿? :wink: thanks again !

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?

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

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?

yes! here it is

i´m calling the Categories
thanks!!

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.

1 Like

sorry i didnt´comeback to you it works, thanks

1 Like