【问题标题】:Node-red:node-function - missing "null" value in if conditionNode-red:node-function - if 条件中缺少“null”值
【发布时间】:2018-04-27 08:22:10
【问题描述】:

我正在使用 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 对象]

并且它总是进入 if 语句(当 "res[0][[key]]" 为空时也是如此)。

我错了什么?

【问题讨论】:

    标签: node.js if-statement null node-red


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-11-13
      • 2017-10-01
      • 1970-01-01
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多