代码之家  ›  专栏  ›  技术社区  ›  Jéan

node red如何通过switch&case将json对象传递给多输出函数节点

  •  0
  • Jéan  · 技术社区  · 6 年前

    要使用具有多个输出的功能节点,可以按如下方式将消息传递到右侧输出:

    switch (msg.payload) {
       case 1:
          var msg1 = {payload: foo.bar} ;
          break;
       case 2: 
          var msg2 = {payload: foo.bar2}
       break;
    }
    return [msg1,msg2];
    

    但是,如果我想通过msg1/msg2传递json对象,它不会传递任何内容。以下两种方法都无法将json对象传递到每个msg的

    var msg1 = {'payload.item1': xx, 'payload.item2': xy}; // or
    var msg1 = {payload: {'item1': xx, 'item2': xy}};
    

    知道如何将对象传递到多输出函数中吗?

    这也不起作用:

    var msg1 = 'a';
    var msg2 = 'b';
    var msg3 = 'c';
    
    switch (msg.payload){
        case "1":
            msg1 = {payload: {'foo':1,'bar':10}};
            break;
        case "2":
           msg2 = {payload: {'foo':2,'bar':12}};
           break;
        case "3":
           msg1 = {payload: {'foo':3,'bar':13}};
           break;
    }
    
    return [msg1,msg2,msg3];
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   AIOT MAKER    6 年前

    上一个例子不起作用的原因很简单:您声明了msg1、msg2、msg3并为它们分配了字符串。节点red希望始终返回对象,而不是字符串。

    因此,如果您替换:

    var msg1 = 'a';
    var msg2 = 'b';
    var msg3 = 'c';
    

    签署人:

    var msg1 = {};
    var msg2 = {};
    var msg3 = {};
    

    运行流时不会看到错误,但是..也许这不是你想要的。为了提供更准确的响应,有必要询问您希望在开关箱未选择的两个输出上交付什么?

    一个更常见的解决方案,但不符合您的要求(使用具有多个输出的功能节点)将使用节点红色开关节点,如下流所示:

    [{"id":"741aef4.6e6c91","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"5adb5fe6.46687","type":"switch","z":"741aef4.6e6c91","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"3","vt":"str"}],"checkall":"false","repair":false,"outputs":3,"x":330,"y":160,"wires":[["daac55db.1fcbc8"],["c1d473f8.3e399"],["46b134bc.88338c"]]},{"id":"51cdd5fa.e5c28c","type":"inject","z":"741aef4.6e6c91","name":"","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":120,"wires":[["5adb5fe6.46687"]]},{"id":"235d0c65.794ed4","type":"inject","z":"741aef4.6e6c91","name":"","topic":"","payload":"2","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":160,"wires":[["5adb5fe6.46687"]]},{"id":"134e5583.3f7a4a","type":"inject","z":"741aef4.6e6c91","name":"","topic":"","payload":"3","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":200,"wires":[["5adb5fe6.46687"]]},{"id":"daac55db.1fcbc8","type":"change","z":"741aef4.6e6c91","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"foo\":1,\"bar\":10}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":140,"wires":[["50f4c1a9.ea486"]]},{"id":"c1d473f8.3e399","type":"change","z":"741aef4.6e6c91","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"foo\":2,\"bar\":12}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":180,"wires":[["cad7dc88.0db57"]]},{"id":"46b134bc.88338c","type":"change","z":"741aef4.6e6c91","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"foo\":3,\"bar\":13}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":220,"wires":[["6da7761e.5c70f8"]]},{"id":"50f4c1a9.ea486","type":"debug","z":"741aef4.6e6c91","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":660,"y":140,"wires":[]},{"id":"cad7dc88.0db57","type":"debug","z":"741aef4.6e6c91","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":660,"y":180,"wires":[]},{"id":"6da7761e.5c70f8","type":"debug","z":"741aef4.6e6c91","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":660,"y":220,"wires":[]}]
    

    enter image description here