Hi there,
i m a newbee and try to execute a function code in node red.
my function is supposed to return me an array but instead i got the message error "Function tried to send a message of type string"
Here is my code
var out = ;
for (var i = 0; i < msg.payload.length; i++) {
if (msg.payload[i].col2 !==null)
{
if (msg.payload[i].col2=="x" )
{
var tmp= msg.payload[i].col1;
out.push(tmp);
}
}
}
return [out];
so the goal is to go through an csv sheet which has 2 column, the first one containing a list of attribute and the second one containing cross. i want to extract only the column 1 attribute where the corresponding column 2 has a cross
Hi,
thanks for your answer.
but is an array not an object?
i tried this "return { payload:out };" but it is not returning anything. I guess it is because my current message does not have a payload value?
Understood thanks.
but still returning nothing
i don t know if i miss something else...
var out = ;
for (var i = 0; i < msg.payload.length; i++) {
if (msg.payload[i].col2 !==null)
{
if (msg.payload[i].col2=="x" )
{
var tmp= msg.payload[i].col1;
out.push(tmp);
I don't see any way that can return nothing. What do you see if you feed it into a debug node ? Also what do you see in a debug node showing the input to the function?
If you actually mean that the message payload sent is an empty array then probably there are no records that pass the tests.
You can debug your function using node.warn(), for example, at the beginning you could put
You can tidy your function up a bit as follows if you like
var out = [];
for (var i = 0; i < msg.payload.length; i++) {
if (msg.payload[i].col2 !==null && msg.payload[i].col2=="x" )
{
out.push( msg.payload[i].col1);
}
msg.payload = out;
return msg;
In fact you don't even need the null test as msg.payload[i].col2 will evaluate to undefined if it does not exist so it will fail the test even if it is null. You would need it in some languages but not javascript.