[英]How do I write a json list with objects in javascript
Hello Guys I have a small problem with writing my list.大家好,我在写我的清单时遇到了一个小问题。
I got this out of the debug tab and i only want to have one output without undefind.我从调试选项卡中得到了这个,我只想拥有一个没有未定义的 output。
I used Node-Red this is my code我使用 Node-Red 这是我的代码
This is the code of the function Set Data这是function设置数据的代码
var number1 = msg.payload.kinput var number2 = msg.payload.kinout var newlisencenumber = '{' + '"number1json"' + ':' + number1 +','+'"number2json"'+':'+ number2 +'}' //------------------------------------------------------------- var prepared = '{' + '"storage"' + ':' + '['+ newlisencenumber + ']' + '}' //------------------------------------------------------------- var msgout = prepared msg.payload = msgout return msg;
At least here is the code for the Node-Red flow至少这里是 Node-Red 流程的代码
[
{
"id": "b071a31b549f930b",
"type": "tab",
"label": "Flow 2",
"disabled": false,
"info": "",
"env": []
},
{
"id": "1b26b618f4eb0d4e",
"type": "inject",
"z": "b071a31b549f930b",
"name": "",
"props": [
{
"p": "payload.kinput",
"v": "\"A1234\"",
"vt": "str"
},
{
"p": "payload.kinout",
"v": "\"in\"",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 270,
"y": 300,
"wires": [
[
"45ab02267afb7af9",
"ede6741db6b1fc08"
]
]
},
{
"id": "ede6741db6b1fc08",
"type": "function",
"z": "b071a31b549f930b",
"name": "Set Data",
"func": "var number1 = msg.payload.kinput\nvar number2 = msg.payload.kinout\n\nvar newlisencenumber = '{' + '\"licensenumber\"' + ':' + number1 +','+'\"inout\"'+':'+ number2 +'}'\n\n//-------------------------------------------------------------\nvar prepared = '{' + '\"storage\"' + ':' + '['+ newlisencenumber + ']' + '}'\n\n//-------------------------------------------------------------\nvar msgout = prepared\n\n\n \n\n\nmsg.payload = msgout\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 840,
"y": 360,
"wires": [
[
"5cbe08fbd89b1b22"
]
]
},
{
"id": "77a032844ea6a095",
"type": "inject",
"z": "b071a31b549f930b",
"name": "",
"props": [
{
"p": "payload.kinput",
"v": "A1234",
"vt": "str"
},
{
"p": "payload.kinout",
"v": "out",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 270,
"y": 380,
"wires": [
[
"45ab02267afb7af9",
"ede6741db6b1fc08"
]
]
},
{
"id": "d8a6455136577088",
"type": "inject",
"z": "b071a31b549f930b",
"name": "",
"props": [
{
"p": "payload.kinput",
"v": "A4321",
"vt": "str"
},
{
"p": "payload.kinout",
"v": "in",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 270,
"y": 340,
"wires": [
[
"45ab02267afb7af9",
"ede6741db6b1fc08"
]
]
},
{
"id": "f6a8cc6165483098",
"type": "inject",
"z": "b071a31b549f930b",
"name": "",
"props": [
{
"p": "payload.kinput",
"v": "A4321",
"vt": "str"
},
{
"p": "payload.kinout",
"v": "out",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 270,
"y": 420,
"wires": [
[
"45ab02267afb7af9",
"ede6741db6b1fc08"
]
]
},
{
"id": "45ab02267afb7af9",
"type": "file in",
"z": "b071a31b549f930b",
"name": "",
"filename": "C:\\test\\test.json",
"filenameType": "str",
"format": "utf8",
"chunk": false,
"sendError": false,
"encoding": "none",
"allProps": false,
"x": 500,
"y": 360,
"wires": [
[
"ede6741db6b1fc08"
]
]
},
{
"id": "5cbe08fbd89b1b22",
"type": "debug",
"z": "b071a31b549f930b",
"name": "debug 6",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 1020,
"y": 360,
"wires": []
}
]
Your code is typecasting the values to string.您的代码正在将值类型转换为字符串。 Now in this case undefined values will convert to a string "undefined".
现在在这种情况下,未定义的值将转换为字符串“未定义”。
You could do something like this:你可以这样做:
// Basically add a fallback of empty string
var number1 = msg?.payload?.kinput || "";
var number2 = msg?.payload?.kinout || "";
So if the value is undefined or even null it will fallback to an empty string so you should get something like this因此,如果该值未定义甚至 null 它将回退到一个空字符串,所以你应该得到这样的东西
"{"storage": [{ "licensenumber": "", "input": "" }]}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.