# How can I split a message?

**URL:** <https://discourse.nodered.org/t/how-can-i-split-a-message/26396>\
**Category:** General\
**Created:** [9 May 2020 23:48 UTC](https://discourse.nodered.org/t/how-can-i-split-a-message/26396 "2020-05-09T23:48:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![asdf123](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@asdf123](https://discourse.nodered.org/u/asdf123)\
**Post date:** [9 May 2020 23:48 UTC](https://discourse.nodered.org/t/how-can-i-split-a-message/26396/1 "2020-05-09T23:48:37Z")

</div>

Hello,

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

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

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

```

How can I fix this?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [9 May 2020 23:57 UTC](https://discourse.nodered.org/t/how-can-i-split-a-message/26396/2 "2020-05-09T23:57:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![asdf123](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@asdf123](https://discourse.nodered.org/u/asdf123)\
**Post date:** [10 May 2020 00:05 UTC](https://discourse.nodered.org/t/how-can-i-split-a-message/26396/3 "2020-05-10T00:05:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [10 May 2020 00:26 UTC](https://discourse.nodered.org/t/how-can-i-split-a-message/26396/4 "2020-05-10T00:26:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![asdf123](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@asdf123](https://discourse.nodered.org/u/asdf123)\
**Post date:** [10 May 2020 00:31 UTC](https://discourse.nodered.org/t/how-can-i-split-a-message/26396/5 "2020-05-10T00:31:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 May 2020 05:59 UTC](https://discourse.nodered.org/t/how-can-i-split-a-message/26396/6 "2020-05-10T05:59:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 July 2020 05:59 UTC](https://discourse.nodered.org/t/how-can-i-split-a-message/26396/7 "2020-07-09T05:59:11Z")

</div>

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