Access number of array

good Morning,
and again I have a problem with reading out the data that I want to read out.
In the picture I see the result: Payload: array[0]

I would like to respond to that!

e.g. If (the array has the number > 0){
Then do it}
else
{ //do nothing}

so far I have tried with msg.payload.length

I also output the payload length under the name payloadlaenge. The number 81 is there. Why? There should be 0 for comparison.

Where is my mistake in thinking?

Thanks in advance
Screenshot 2023-09-17 112535

if(msg.payload.length === 0){
 do stuff....
}else{
   ... do something else
}

Arrays have a length property you can read about arrays here Array - JavaScript | MDN
[edit fixed typo in length.]
If you wish a function to not output and do nothing return null, you can do this by setting msg = null;

thx for your fast replay! i test it ..

Unfortunately this did not work.
There are only 2 possibilities which can be.
In the first picture the array has a 0
In the second picture the array has a 1

But the payload length never shows me 0 or 1.

hat keinen inhalt

hat einen inhalt

I think the error is that I don't need the length of the payload but the number of objects in the payload's array. Can this be?

To explain the whole thing a little better if necessary:
I check in a database whether a data record already exists or not.

If the data record does not yet exist, I get the following in the debug window:
object
payload: array[0]

In this case the data record should be added.

If the data record already exists I get in the debug window:
object
payload: array[1]
In this case, the data record already exists and nothing needs to be done.

If you use it correctly it will work
see this example

[{"id":"d949b60aacba0c90","type":"inject","z":"b9860b4b9de8c8da","name":"array length 0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[]","payloadType":"jsonata","x":330,"y":140,"wires":[["ee9b01abf97044ae","837e4484190e5395"]]},{"id":"ee9b01abf97044ae","type":"debug","z":"b9860b4b9de8c8da","name":"debug 338","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":80,"wires":[]},{"id":"837e4484190e5395","type":"function","z":"b9860b4b9de8c8da","name":"function 21","func":"if(msg.payload.length === 0){\n    msg.payload =\"length 0\";\n}else{\n    msg.payload =\"length 1\";\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":140,"wires":[["9d0d9f4b63e71d9b"]]},{"id":"12a8804aedf9931f","type":"inject","z":"b9860b4b9de8c8da","name":"array legth 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"test\":1}]","payloadType":"jsonata","x":330,"y":180,"wires":[["ee9b01abf97044ae","837e4484190e5395"]]},{"id":"9d0d9f4b63e71d9b","type":"debug","z":"b9860b4b9de8c8da","name":"debug 339","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":80,"wires":[]}]

Or to do nothing when array length is 1

[{"id":"d949b60aacba0c90","type":"inject","z":"b9860b4b9de8c8da","name":"array length 0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[]","payloadType":"jsonata","x":330,"y":140,"wires":[["ee9b01abf97044ae","837e4484190e5395"]]},{"id":"ee9b01abf97044ae","type":"debug","z":"b9860b4b9de8c8da","name":"debug 338","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":80,"wires":[]},{"id":"837e4484190e5395","type":"function","z":"b9860b4b9de8c8da","name":"function 21","func":"if(msg.payload.length === 0){\n    msg.payload =\"length 0\";\n}else{\n    msg = null;\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":140,"wires":[["9d0d9f4b63e71d9b"]]},{"id":"12a8804aedf9931f","type":"inject","z":"b9860b4b9de8c8da","name":"array legth 1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"test\":1}]","payloadType":"jsonata","x":330,"y":180,"wires":[["ee9b01abf97044ae","837e4484190e5395"]]},{"id":"9d0d9f4b63e71d9b","type":"debug","z":"b9860b4b9de8c8da","name":"debug 339","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":80,"wires":[]}]

How to import/export a flow

If you require more help please supply a test flow like i have that show the issue you are getting.

THANK YOU !

IT WORKS!

I have it a little bit changed

if (msg.payload.length === 0) {
    msg.topic = "INSERT INTO eq5q (link) VALUES ('" + msg.payloadpruef + "')";
    return msg;
} else {
    //mach nix
}

Check whether your SQL variant has an INSERT IF NOT EXISTS feature. Using that will be much more efficient.

If you want to do it that way the else is superflulous, just do

if (msg.payload.length === 0) {
    msg.topic = "INSERT INTO eq5q (link) VALUES ('" + msg.payloadpruef + "')";
    return msg;
} 

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.