Hi all,
I'm fairly new to Node-RED and totally unfamiliar with javascript. Who can help me with my code?
I'm trying to parse a WooCommerce webhook and send some information to my phone thru join (joaoapps).
I searched the web for solutions and I think i'm almost there. I found this example on stackoverflow and hacked up this little code on https://js.do/code/json_iteration.
This displays the result what i'm looking for.
When i'm trying to translate this into Node-RED, i'm only getting the last part (Book D) of the array.
Here's my flow code:
[
{
"id": "cc4c4710.0e42f8",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": ""
},
{
"id": "6b97b4d9.508d9c",
"type": "inject",
"z": "cc4c4710.0e42f8",
"name": "Test JSON inject",
"topic": "",
"payload": "{\"status\":\"completed\",\"total\":\"80.50\",\"line_items\":[{\"name\":\"Book A\",\"quantity\":1},{\"name\":\"Book B\",\"quantity\":2},{\"name\":\"Book C\",\"quantity\":3},{\"name\":\"Book D\",\"quantity\":4}]}",
"payloadType": "json",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 240,
"y": 160,
"wires": [
[
"71becfa3.e7d4b"
]
]
},
{
"id": "71becfa3.e7d4b",
"type": "switch",
"z": "cc4c4710.0e42f8",
"name": "",
"property": "payload.status",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "completed",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 250,
"y": 260,
"wires": [
[
"b082823.37b478"
]
]
},
{
"id": "b082823.37b478",
"type": "function",
"z": "cc4c4710.0e42f8",
"name": "Message format",
"func": "var arr = msg.payload.line_items;\n\nfor (var i = 0; i < arr.length; i++){\n var obj = arr[i];\n book = (\"\\n What: \" + obj.name);\n amount = (\"\\n How many: \" + obj.quantity);\n}\n\nmoney = msg.payload.total;\nmsg.title = \"Books sold!\";\nmsg.text = book + amount + \"\\n\\n Total money earned: \" + money\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 260,
"y": 360,
"wires": [
[
"49943e66.fc353"
]
]
},
{
"id": "49943e66.fc353",
"type": "debug",
"z": "cc4c4710.0e42f8",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"x": 270,
"y": 460,
"wires": []
}
]
The WooCommerce JSON output is redacted of course, and the switch node is there to only display when a order is completed.