Node-red:node-function - if 条件中缺少 "null" 值

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

我正在使用 Node-Red,在节点函数中我有:

  ...  
  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]]);  
  }  

在 console.log 我一直:

res: 0:[object Object]

它总是进入 if 语句(当 "res[0][[key]]" 为 null 时)。

我错了什么?

Object.keys returns 包含对象键的数组。您的代码使用的是整个数组,而不是其中的一个值。

为了获得 pressure 的值,您将使用:

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