Search code examples
node.jsif-statementnullnode-red

Node-red:node-function - missing "null" value in if condition


I'm using Node-Red and in node function I have:

  ...  
  res = dataTransform.transform();  
  //critic case: res = [{"pressure":null}];  
  key = Object.keys(res[0]);  
  if(res[0][[key]]!=null)  
  {  
   ...   
   console.log("res:   ", [key]+":"+res[0][[key]]);  
  }  

on console.log I have always:

res: 0:[object Object]

And it enters in if-statement always (also when the "res[0][[key]]" is null).

What was I mistake?


Solution

  • Object.keys returns an array containing the keys of the Object. Your code is using that entire array rather than a value from within it.

    In order to get to the value of pressure, you would use:

    var keys = Object.keys(res[0]);
    var key = keys[0];
    if (res[0][key] != null) {
        console.log(res[0][key]);
    }