# Can you return a msg object as a pure array?

**URL:** <https://discourse.nodered.org/t/can-you-return-a-msg-object-as-a-pure-array/23033>\
**Category:** General\
**Created:** [12 March 2020 13:35 UTC](https://discourse.nodered.org/t/can-you-return-a-msg-object-as-a-pure-array/23033 "2020-03-12T13:35:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![etawyy](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@etawyy](https://discourse.nodered.org/u/etawyy)\
**Post date:** [12 March 2020 13:35 UTC](https://discourse.nodered.org/t/can-you-return-a-msg-object-as-a-pure-array/23033/1 "2020-03-12T13:35:11Z")

</div>

Hi  
I need my msg object (not msg.payload) to be an array.  
This is for a cloudant nodes that can make bulk inserts "if the incoming msg is an array."

> If `msg` is an array, all elements will be processed using the **bulk operations API**. (cloudantplus node)

So from a msg with msg.payload=[the array I want], I'd like to use a function node to get in the output: msg=[the array I want].

Is this possible? I have tried the obvious:

```auto
let my_array = msg.payload;
return my_array; // I get only a msg with the first array member

```

```auto
let my_array = msg.payload;
return [my_array]; //This will send as many messages as array members individually

```

```auto
let my_array = msg.payload;
msg=[]; //to erase it;
msg=my_array;
return msg; // I get only a msg with the first array member

```

Any solution to 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:** [12 March 2020 13:49 UTC](https://discourse.nodered.org/t/can-you-return-a-msg-object-as-a-pure-array/23033/2 "2020-03-12T13:49:20Z")

</div>

It is not possible for a message to be an array.

The cloudantplus node's bulk data option only applies if it is saving `msg.payload` - you cannot use it with the 'whole message' option.

So pass in `msg.payload` as the array of values you want it to store.

---

<div class="post-metadata">

**Author:** ![etawyy](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@etawyy](https://discourse.nodered.org/u/etawyy)\
**Post date:** [12 March 2020 14:00 UTC](https://discourse.nodered.org/t/can-you-return-a-msg-object-as-a-pure-array/23033/3 "2020-03-12T14:00:52Z")

</div>

Ok thanks - but then the cloudantplus node doesn't seem to work. I had actually tried before and the node will insert only one document with the complete array in it. instead of several documents:

```auto
{
  "0": {
    "message": "ZZZ",
    "type": "network",
    "code": 3202
  },
  "1": {
    "message": "XXX",
    "type": "network",
    "code": 3402
  },
  "_id": "f0eb1eed71389a514d8de7fcf47c8390",
  "_rev": "1-f246b9fe183a1b95466822884b1e77da"
}

```

---

<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:** [12 March 2020 14:06 UTC](https://discourse.nodered.org/t/can-you-return-a-msg-object-as-a-pure-array/23033/4 "2020-03-12T14:06:31Z")

</div>

I haven't used the node myself, just going by what I can see in the code.

How are you setting `msg.payload`?

---

<div class="post-metadata">

**Author:** ![etawyy](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@etawyy](https://discourse.nodered.org/u/etawyy)\
**Post date:** [12 March 2020 14:11 UTC](https://discourse.nodered.org/t/can-you-return-a-msg-object-as-a-pure-array/23033/5 "2020-03-12T14:11:46Z")

</div>

Just before the cloudantplus node, msg.payload is indeed an array:

payload: array[2]

> 0: object  
> 1: object

And just before that it was set by moving (in a change node) the data from another msg.logs object, which itself was created before by a serie of msg.logs.push actions.

---

<div class="post-metadata">

**Author:** ![etawyy](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@etawyy](https://discourse.nodered.org/u/etawyy)\
**Post date:** [12 March 2020 15:18 UTC](https://discourse.nodered.org/t/can-you-return-a-msg-object-as-a-pure-array/23033/6 "2020-03-12T15:18:43Z")

</div>

Well, I took the cloudantplus code, and ran it simply in a function node.  
At least all the way to bulkDocument() function, to check the "doc" document. And it is the right array, even after parseMessage() function.  
So everything seems ok just before passing the bulk command.

I don't get it.

doc: array[2]  
0: object  
1: object

data: array[2]  
0: object  
1: object

type: "object"  
proto: "[object Array]"

form cloudantplus code:

```auto
var data = msg.payload;
delete data._msgid;
var root = "payload";
var doc = parseMessage(data, root);
msg.doc=doc;
msg.data = data;
msg.type = typeof msg;
msg.proto = Object.prototype.toString.call( doc );

return msg;
<with all the functions below (skipping here)>

```

---

<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:** [11 May 2020 15:18 UTC](https://discourse.nodered.org/t/can-you-return-a-msg-object-as-a-pure-array/23033/7 "2020-05-11T15:18:49Z")

</div>

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