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:
let my_array = msg.payload;
return my_array; // I get only a msg with the first array member
let my_array = msg.payload;
return [my_array]; //This will send as many messages as array members individually
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?
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.
1 Like
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:
{
"0": {
"message": "ZZZ",
"type": "network",
"code": 3202
},
"1": {
"message": "XXX",
"type": "network",
"code": 3402
},
"_id": "f0eb1eed71389a514d8de7fcf47c8390",
"_rev": "1-f246b9fe183a1b95466822884b1e77da"
}
I haven't used the node myself, just going by what I can see in the code.
How are you setting msg.payload
?
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.
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:
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)>
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.