Show me the whole function code with the code I provided & the payload that enters it
Never mind, I see you have edited your previous post.
The code I provided needs to be rearranged. As you are looping through the payload array, you need to test each element is an object and it contains the id property
Also looking at your full function there is quite a bit of optimisation and type checking required.
Unfortunately I am not near a computer so cannot help further until later this afternoon.
I've posted the code before your reply, the error now is TypeError: Cannot read property 'd_id' of undefined
msg.payload = 1
msg.reparto = 1
Unfortunately, the code is a bit messed up by the changes suggested and made on the fly. If we then assume that I am a fairly inexperienced programmer ... However, do not worry, you have already done a lot, if you have some time to fix my code (even just the if of r_id would be enough, the others are practically repeated ) I'll be grateful to you. Obviously without rush and when you want.
Or use modern JavaScript "optional chaining". It is a fairly new js feature so be careful if it is supported by your eco system
if (typeof msg.payload?.[0]?.r_id === 'number') {
// its fine to treat msg.payload[0].r_id as a number
msg.payload = msg.payload[0].r_id + 1;
} else {
msg.payload = "oh no!"
}
return msg;
Funny syntax, JS is getting more and more weirder but nice
Optional chaining is new for me so I made a small test flow
[{"id":"1a438b348c3ccb4c","type":"function","z":"db39bbbe01b608ed","name":"","func":"if (typeof msg.payload?.[0]?.r_id === 'number') {\n // its fine to treat msg.payload[0].r_id as a number\n msg.payload = msg.payload[0].r_id + 1;\n} else {\n msg.payload = \"oh no!\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":400,"wires":[["9f83c3ef47233c05"]]},{"id":"f24d4df3e184ddfe","type":"inject","z":"db39bbbe01b608ed","name":"no payload","props":[{"p":"time","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":160,"y":320,"wires":[["1a438b348c3ccb4c"]]},{"id":"9f83c3ef47233c05","type":"debug","z":"db39bbbe01b608ed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":510,"y":400,"wires":[]},{"id":"b1fc4aa7a78be3e5","type":"inject","z":"db39bbbe01b608ed","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":150,"y":360,"wires":[["1a438b348c3ccb4c"]]},{"id":"0ad05a2c15de4764","type":"inject","z":"db39bbbe01b608ed","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"notThis\":1}]","payloadType":"json","x":170,"y":400,"wires":[["1a438b348c3ccb4c"]]},{"id":"8f9eea1c02ff4651","type":"inject","z":"db39bbbe01b608ed","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"r_id\":1}]","payloadType":"json","x":160,"y":440,"wires":[["1a438b348c3ccb4c"]]}]
With this I give TypeError: Cannot read property 'd_id' of undefined
with this input:
msg.payload=1
msg.reparto=1
Until now I don't resolve but I learn lot of stuff
Sorry but I can't confirm any error here:
Clicking through all inject nodes ...
in this demo:
[{"id":"1a438b348c3ccb4c","type":"function","z":"db39bbbe01b608ed","name":"","func":"if (typeof msg.payload?.[0]?.r_id === 'number') {\n // its fine to treat msg.payload[0].r_id as a number\n msg.payload = msg.payload[0].r_id + 1;\n} else {\n msg.payload = \"oh no!\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":400,"wires":[["9f83c3ef47233c05"]]},{"id":"f24d4df3e184ddfe","type":"inject","z":"db39bbbe01b608ed","name":"no payload","props":[{"p":"time","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":160,"y":320,"wires":[["1a438b348c3ccb4c","30bc62ede6eb71fd"]]},{"id":"9f83c3ef47233c05","type":"debug","z":"db39bbbe01b608ed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":510,"y":400,"wires":[]},{"id":"b1fc4aa7a78be3e5","type":"inject","z":"db39bbbe01b608ed","name":"msg.payload=1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":140,"y":360,"wires":[["1a438b348c3ccb4c","30bc62ede6eb71fd"]]},{"id":"0ad05a2c15de4764","type":"inject","z":"db39bbbe01b608ed","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"notThis\":1}]","payloadType":"json","x":150,"y":400,"wires":[["1a438b348c3ccb4c","30bc62ede6eb71fd"]]},{"id":"8f9eea1c02ff4651","type":"inject","z":"db39bbbe01b608ed","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"r_id\":1}]","payloadType":"json","x":160,"y":480,"wires":[["1a438b348c3ccb4c","30bc62ede6eb71fd"]]},{"id":"229752227711f9ea","type":"inject","z":"db39bbbe01b608ed","name":"msg.reparto=1","props":[{"p":"reparto","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":140,"y":440,"wires":[["1a438b348c3ccb4c","30bc62ede6eb71fd"]]},{"id":"30bc62ede6eb71fd","type":"debug","z":"db39bbbe01b608ed","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":350,"y":460,"wires":[]}]
My be a typo r_id
or d_id
?
I tried your flow by importing it into the project that gives me that problem and it works fine. I tried to completely modify my code using your method and I don't get any more errors ... Maybe I'm tired, maybe I'm stupid but obviously I must have done something wrong this morning, most likely the solutions proposed by the other members would have worked if implemented by me correctly ... I apologize to everyone, I have wasted your time, the only consolation is that I have learned many new ways to identify data types, which will surely come in very handy.
I apologize again and thank you very much for your patience and help.
No worries. I had an opportunity to test "optional chaining" a JS feature i had read about on the go but never tested.
Checking for undefined properties or trowing errors because of accessing them without proper checks is a common problem of lose typed languages like JavaScript. Some people say that half of their code is only checking for potential problems. So optional chaining seams to be a possibility to reduce this long chains of checks and make them more readable.
I have been learning java script for a few months and I am realizing that compared to a C language there are many simplifications but these simplifications if not managed correctly lead to problems that for a noob like me require time and continuous research to be solved. I will definitely have to study more. Thanks again for your patience and for the very useful information that will surely not be lost
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.