Kulasangar
Kulasangar

Reputation: 9444

How to extract a msg object which I'm getting from a http request?

I'm still a newbie to this. What I have done is, I've tried sending an http request and I'm trying to pass the below response via my custom node. The msg object which looks like:

[
  {
    "userId": 1,
    "deviceId": 2,
    "type": "SM",
    "eventName": "TemparatureChanged",
    "stateName": "update",
    "eventParameters": [
      {
        "name": "temparature",
        "type": "Double",
        "value": 35.01
      }
    ]
  }
]

How can I retrieve the value from the eventParameters array above?

I tried the below in a function node:

var data  = msg.eventParameters[0].value;
return data;

And I want to only print the above property through the debug node. My debug node contains msg.payload, also I tried having it as a complete msg object still no luck.

And in my custom node (IOT-Input) js file I've got this:

this.on('input', function (msg) {
            node.warn("I saw a payload: "+msg.payload);
            // in this example just send it straight on... should process it here really
            node.send(msg);
});

My flow looks like this:

enter image description here

Where am I going wrong? Any help could be appreciated!

Upvotes: 0

Views: 945

Answers (2)

hardillb
hardillb

Reputation: 59686

You don't say where you got your sample msg from at the top of the question but if it can from the console.log in your node, then what you have is the msg.payload not the msg.

So you should be using something like:

var data = msg.payload[0].eventParameters[0].value;

And also you shouldn't be just returning data from the function node, it should be a msg object so something like this:

var data = msg.payload[0].eventParameters[0].value;
msg.payload = data;
return msg.payload;

Upvotes: 1

michelem
michelem

Reputation: 14590

msg is an array so it should be:

var data  = msg[0].eventParameters[0].value;

Upvotes: 1

Related Questions