I'm new to javascript and nodeRED. I'm working in Visual Flow Creator and trying to take 4 values:
var msg = {
payload: {
Dryblend_Dose_Finished: true, // Sample values, could be dynamically set
Filler_Dose_Finished: true,
Weight_Sent_DryBlend: 100, // Sample values, could be dynamically set
Weight_Sent_Filler: 50
}
};
...and when the 2 BOOLean vars are both TRUE, add the 2 "Weight..." values together, storing the result in a new property that will be added to the msg.payload array.
My Code:
// Bring in the payload
var array = msg.payload;
// Check to see if the BOOLs are both TRUE
if (array.Dryblend_Dose_Finished === "true" && array.Filler_Dose_Finished === "true")
{
//add the 2 weights together and store for incorporation into the outgoing msg.payload
array.total = array.Weight_Sent_DryBlend + array.Weight_Sent_Filler;
}
else {
array.didntWork = 1;
}
// put everything back into the payload
msg.payload = array;
return msg;
It seems to me like this should be "simple" and yet, no matter what I do, it seems like it simply doesn't even run. In my debug node after the function block, I get the same values out that went in with no changes. I added the "didntWork" var thinking I might sanity check my logic, but that doesn't show up either, so now I don't even know if the stupid function is even running (except that nodeRED/Visual Flow Creator says the function completed in 4.08ms.
Anyone have any ideas? This should not be that difficult, and yet this refuses to work.
What am I doing wrong?
The answer has already been given in the comments: Your code is syntactically correct, but there is a problem with your boolean expression. The way you are doing it will never behave as you would expect because you are comparing different types (boolean with string).
The variables
Dryblend_Dose_FinishedandFiller_Dose_Finishedare already booleans, so you don't have to compare them. The boolean expression is as simple as(array.Dryblend_Dose_Finished && array.Filler_Dose_Finished).Besides that, there are minor issues with your variable names. What you call an array is not really an array in the sense of Javascript. An array looks like this:
var pets = ['dog', 'cat', 'rabbit']and you can access the items via their index likepets[1](which results in 'cat'). Propertypayloadof objectmsgis an object itself which consists of the boolean propertiesDryblend_Dose_FinishedandFiller_Dose_Finishedand the numeric propertiesWeight_Sent_DryBlendandWeight_Sent_Filler. So, naming the variable an array may be misleading. In Javascript it is also a widely adopted to use camelCase naming instead of using '_' as a separator. Your variables would then look likeweigthSentFillerorweightSentDryBlend. But not sure if you can influence the format of the message.In addition, it is good practise to begin names of boolean variables with the verbs is or has like in
isFillerDoseFinishedornode.hasChildren. Using these naming conventions makes your code better readable and understandable. You would see at the first glance thatisFillerDoseFinishedis a boolean and that you don't have to check for equality in a boolean expression.I have tested your code live in Node-RED by using an inject node with the following JSON input as payload:
This results in exactly the object that you used in your example (see screenshot debug output before). Then I added a function node with your code, corrected the boolean expression and deployed it. As you can see in the debug output after, the new property
total: 150was added to the payload.I hope that helps :)