Search code examples
mysqlnode.jsnode-red

Get value from msg.payload return from a mysql query Node-RED


i am starting with using Node-RED. i have no JS experience.

in 1 node for the mysql DB i have to query with msg.topic.

the return msg looks like this.

{ "topic": "SELECT sum FROM log WHERE id = 2", "payload": [ { "sum": 15 } ], "_msgid": "c93094ee.36cf68" }

How do i extract the number 15 in the example out msg.payload?

thanks.


Solution

  • in function node following the query.

    var sum  = msg.payload[0].sum;
    ...
    

    the [] in the payload signify and array of answers so the [0] says take the first element from the array.