It should just be msg.payload.value
in the groov write node, since that’s what you are returning in this object:
node.send({ payload: { tagName : 'Class ID',
tableStartIndex : i,
value : msg.payload[i] }});
This will send a msg
object that has one property, the payload
. The payload
has three subproperties; tagName
, tableStartIndex
, and value
.
So you just want to grab this msg.payload.value
and stop there – that value
property has no subproperties, so msg.payload.value.value
is undefined, and then you get the error on the property Id because you can’t get a property of an undefined object.
Hopefully that makes some sense and gets you up and running!
Excellent! That was it. Thank you.
Sorry, premature celebration.
Now I am getting this error and no writes except the "0"s
Bad request. HTTP response error : 400
Here is the output before the groov write:
6/2/2021, 12:57:41 PM[node: 4b5bfc4d.fb0f1c](https://192.168.1.90/node-red/#)
msg.payload.value : Object
object
tagName: "Class ID"
tableStartIndex: 1
value: object
Url: "http://192.168.1.198:88/api/v0/classes/12901679107"
Id: 12901679107
If I join them together like this all I get is the 0’s.
var sentence = msg.payload.obj.Classes; // Some table/array of data.
var data = []; // A new array to hold the tag, indices, and values for groov.
for (i = 0; i < sentence.length; i++) // Go through the list
{
data.push({
tagName : 'Class ID',
tableStartIndex : i,
value : sentence[i]
}); // Each 'data' object will be one groov write
}
//return { payload : data }; // This new array is the payload, soon to be split!
for(i = 0; i < 41; i++) {
// If this value exists, write it to the list:
if(typeof(msg.payload[i]) !== "undefined") {
node.send({ payload: { tagName : 'Class ID',
tableStartIndex : i,
value : msg.payload[i] }});
}
// Write zero for undefined values and values not provided by the payload:
else node.send({ payload: { tagName : 'Class ID',
tableStartIndex : i,
value : 0 }});
}
When the code is combined and ran back-to-back like this your data is not in msg.payload[i]
anymore, it’s in data[i]
. It was only in msg.payload as an array becuase you returned it from the previous node when you did return { payload : data }
It can still work if the code is combined like this, you just need to be mindful of variable names when moving code around into different scopes and make sure it’s consistent.
If that fix isn’t enough, I would start by doing some deeper, more specific debugging with node.warn();
(You can see more about logging events on this page: https://nodered.org/docs/user-guide/writing-functions.)
For example, is sentence being correctly filled with the values you expect?
You could check that before the loop with node.warn(sentence[0]);
or even node.warn(sentence[i]);
inside the first loop, or check data[i]
further down, depending on where it goes bad. Narrowing down the line that things go wrong is a really important part of debugging.
Also, I noticed you’re going from i=0
to i<41
in the second loop – that is a total of 41 values including 0, not 40 – which is OK if you have that many indexes in your groov array, I just wanted to point it out incase that was unintentional.
I figured it out. I needed to look at
msg.payload[i]
for ‘undefined’, but write
msg.payload[i].value.Id
as a value. So the second node looks like this:
for(i = 0; i < 41; i++) {
// If this value exists, write it to the list:
if(typeof(msg.payload[i]) !== "undefined") {
node.send({ payload: { tagName : 'Class ID',
tableStartIndex : i,
value : msg.payload[i].value.Id}});
}
// Write zero for undefined values and values not provided by the payload:
else node.send({ payload: { tagName : 'Class ID',
tableStartIndex : i,
value : 0 }});
}
Yes, the 0 to 41 was on purpose so I could start at slot 1 and fill to slot 40.
(actually thanks for the catch, I needed to start at i=1)