How can I split a message?

Hello,

I have a function node, which return an array as a message:

var r = /\W+/;
msg.payload = msg.payload.split(r);
var msg1 = { payload: msg.payload.length }
msg1.payload = "";
for (var i = 0; i < msg.payload.length-1; i++) 
{
    if (msg.payload[i] == "ID" && msg.payload[i+1] == "CVE") 
    {
     msg1.payload += msg.payload[i+1] + "-" + msg.payload[i+2] + "-" + msg.payload[i+3] + "\n";
    }
}  
    
return msg1;

This is the output:

CVE-2006-4994
CVE-2010-4671
CVE-2011-2834
CVE-2011-2835
CVE-2011-2836
CVE-2011-2837
CVE-2011-2838
CVE-2011-2840
.
.
.
I want to insert it into an sqlite table, but I got "TypeError: msg.payload.split is not a function" error.

The flow looks like this:

inject -> ID function, which I uploaded -> insert function which looks like:

var newMsg = {
    "topic": "INSERT INTO Database VALUES (" + 
        "'"+msg1+ "', " +")"
}
return newMsg;

How can I fix this?

If you are getting an error saying msg.payload.split is not a function, then msg.payload is not a String type.

You don't mention in the question what the input to your function looks like. Wire up a Debug node next to the Function and check what you are passing in.

My input is the output of the 1st function, so an array with strings like:
CVE-2006-4994
CVE-2010-4671
CVE-2011-2834
CVE-2011-2835
CVE-2011-2836
CVE-2011-2837
CVE-2011-2838
CVE-2011-2840

But msg.payload.split appears in the first function, not the second one. So I assumed that was the node reporting the error.

So which node is reporting the error? If the first function, then everything I said before still stands true. You need to check the input to that function to see why msg.payload has no split function.

Yes, you're right. The first function gave the message, because when I tried to connect the first function to the insert function, i didn't connected it to the input. So now it doesn't give that error, but the insert function still can't handle the first function's output, the array.

You could change the first function so it sends a sequence of messages rather than an array, then your second function can add one record at a time. Alternatively insert a Split node to convert the array to a sequence.

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