在Node-RED mongodb2节点中使用JSON.parse()的位置

时间:2019-06-17 17:58:54

标签: mongodb camera raspberry-pi iot node-red

由于JSON节点似乎无法解析msg.payload对象,因此我无法使mongodb2节点存储照片数据。我不确定在哪里可以直接在msg对象上调用JSON.parse(),或者这样做是否正确

我对此进行了一些探索,但正在努力寻找适用的答案。

我看过: https://groups.google.com/forum/#!topic/node-red/0OZcvERZ_8I

TypeError: Cannot create property '_id' on string

MongoDB Error: Cannot create property '_id' on string

TypeError: Cannot create property '_id' on string '{"[object Object]":""}'

我已经尝试在msg.payload上使用JSON.parse,但这似乎是多余的,并且出现了SyntaxError: Unexpected token / in JSON at position 0错误。

原始错误为TypeError: Cannot create property '_id' on string '{"type":"photo","content":"/home/pi/Pictures/photo0.JPEG"}'

这是我修改的流程:

[{"id":"76748518.aa6a6c","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"604458.d3ad0ba8","type":"camerapi-takephoto","z":"76748518.aa6a6c","filemode":"1","filename":"photo0.JPEG","filedefpath":"0","filepath":"/home/pi/Pictures/","fileformat":"jpeg","resolution":"2","rotation":"0","fliph":"0","flipv":"0","brightness":"50","contrast":"0","sharpness":"0","quality":"80","imageeffect":"none","exposuremode":"auto","iso":"0","agcwait":"1.0","led":"0","awb":"auto","name":"","x":340,"y":260,"wires":[["52e79c7a.b67434","2ba7da54.f38006"]]},{"id":"3cbda3b.137155c","type":"inject","z":"76748518.aa6a6c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":200,"wires":[["604458.d3ad0ba8"]]},{"id":"f559bd59.c0ea3","type":"telegram sender","z":"76748518.aa6a6c","name":"","bot":"7e73adde.a70ef4","x":630,"y":380,"wires":[[]]},{"id":"52e79c7a.b67434","type":"function","z":"76748518.aa6a6c","name":"","func":"msg.payload={};\nmsg.payload.chatId=862276680;\nmsg.payload.type=\"photo\";\nmsg.payload.caption=\"Here's the caption\";\nmsg.payload.content= \"/home/pi/Pictures/photo0.JPEG\";\n//msg.payload.type=\"message\";\n//msg.payload.content=\"hello from node-red!\";\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":380,"wires":[["f559bd59.c0ea3"]]},{"id":"2ba7da54.f38006","type":"function","z":"76748518.aa6a6c","name":"","func":"msg.payload=JSON.parse(msg.payload);\n//msg.payload.chatId=862276680;\nmsg.payload.type=\"photo\";\n//msg.payload.caption=\"Here's the caption\";\nmsg.payload.content= \"/home/pi/Pictures/photo0.JPEG\";\n//msg.payload.type=\"message\";\n//msg.payload.content=\"hello from node-red!\";\nreturn msg;","outputs":1,"noerr":0,"x":170,"y":400,"wires":[["58ce8238.e9afdc","12fd6528.5ed4ab"]]},{"id":"58ce8238.e9afdc","type":"debug","z":"76748518.aa6a6c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":540,"y":460,"wires":[]},{"id":"92ebb1ff.b6775","type":"mongodb2 in","z":"76748518.aa6a6c","service":"_ext_","configNode":"e457b3d4.0a36e","name":"","collection":"CameraPhotos","operation":"insertOne","x":480,"y":620,"wires":[["d063a990.254128"]]},{"id":"12fd6528.5ed4ab","type":"json","z":"76748518.aa6a6c","name":"","property":"payload","action":"obj","pretty":false,"x":280,"y":560,"wires":[["92ebb1ff.b6775"]]},{"id":"d063a990.254128","type":"debug","z":"76748518.aa6a6c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":560,"wires":[]},{"id":"7e73adde.a70ef4","type":"telegram bot","z":"","botname":"Kogamibot","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"12","usesocks":false,"sockshost":"","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"verboselogging":true},{"id":"e457b3d4.0a36e","type":"mongodb2","z":"","uri":"mongodb://localhost:27017/local","name":"db","options":"","parallelism":"-1"}]

这是我的原始流程:

[{"id":"604458.d3ad0ba8","type":"camerapi-takephoto","z":"76748518.aa6a6c","filemode":"1","filename":"photo0.JPEG","filedefpath":"0","filepath":"/home/pi/Pictures/","fileformat":"jpeg","resolution":"2","rotation":"0","fliph":"0","flipv":"0","brightness":"50","contrast":"0","sharpness":"0","quality":"80","imageeffect":"none","exposuremode":"auto","iso":"0","agcwait":"1.0","led":"0","awb":"auto","name":"","x":340,"y":260,"wires":[["52e79c7a.b67434","2ba7da54.f38006"]]},{"id":"3cbda3b.137155c","type":"inject","z":"76748518.aa6a6c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":200,"wires":[["604458.d3ad0ba8"]]},{"id":"f559bd59.c0ea3","type":"telegram sender","z":"76748518.aa6a6c","name":"","bot":"7e73adde.a70ef4","x":630,"y":380,"wires":[[]]},{"id":"52e79c7a.b67434","type":"function","z":"76748518.aa6a6c","name":"","func":"msg.payload={};\nmsg.payload.chatId=862276680;\nmsg.payload.type=\"photo\";\nmsg.payload.caption=\"Here's the caption\";\nmsg.payload.content= \"/home/pi/Pictures/photo0.JPEG\";\n//msg.payload.type=\"message\";\n//msg.payload.content=\"hello from node-red!\";\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":380,"wires":[["f559bd59.c0ea3"]]},{"id":"2ba7da54.f38006","type":"function","z":"76748518.aa6a6c","name":"","func":"msg.payload=msg.payload;\n//msg.payload.chatId=862276680;\nmsg.payload.type=\"photo\";\n//msg.payload.caption=\"Here's the caption\";\nmsg.payload.content= \"/home/pi/Pictures/photo0.JPEG\";\n//msg.payload.type=\"message\";\n//msg.payload.content=\"hello from node-red!\";\nreturn msg;","outputs":1,"noerr":0,"x":170,"y":400,"wires":[["58ce8238.e9afdc","12fd6528.5ed4ab"]]},{"id":"58ce8238.e9afdc","type":"debug","z":"76748518.aa6a6c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":540,"y":460,"wires":[]},{"id":"92ebb1ff.b6775","type":"mongodb2 in","z":"76748518.aa6a6c","service":"_ext_","configNode":"e457b3d4.0a36e","name":"","collection":"CameraPhotos","operation":"insertOne","x":480,"y":620,"wires":[["d063a990.254128"]]},{"id":"12fd6528.5ed4ab","type":"json","z":"76748518.aa6a6c","name":"","property":"payload","action":"obj","pretty":false,"x":280,"y":560,"wires":[["92ebb1ff.b6775"]]},{"id":"d063a990.254128","type":"debug","z":"76748518.aa6a6c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":560,"wires":[]},{"id":"7e73adde.a70ef4","type":"telegram bot","z":"","botname":"Kogamibot","usernames":"","chatids":"","baseapiurl":"","updatemode":"polling","pollinterval":"12","usesocks":false,"sockshost":"","socksport":"6667","socksusername":"anonymous","sockspassword":"","bothost":"","localbotport":"8443","publicbotport":"8443","privatekey":"","certificate":"","useselfsignedcertificate":false,"verboselogging":true},{"id":"e457b3d4.0a36e","type":"mongodb2","z":"","uri":"mongodb://localhost:27017/local","name":"db","options":"","parallelism":"-1"}]

0 个答案:

没有答案