Problem while depending of object length

Hello, i'm receiving data which change in every objects, i'd like to make a loop and send 60 differents msg if there's 60 objects for example.
image

this is the code i've done, i managed to get the amount of objects by using the msg.payload.length, and in the new msg.payload i'd like to send what you can see in the screenshot, but just change the "x" by the number of the object, to get the "lat" and "lon" variables changing depending of the objects.

The message get sent only once only with the data of the msg.payload[0] object.

How my msg.payload first look :

Any help would be really apreciated ! Thank you for reading me and i apologize for my english.
My flow code as well:

[
    {
        "id": "67991373.45438c",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": ""
    },
    {
        "id": "4440c71e.f88f18",
        "type": "ui_table",
        "z": "67991373.45438c",
        "group": "fe9b4de4.dcd72",
        "name": "Tableau",
        "order": 11,
        "width": "25",
        "height": "14",
        "columns": [
            {
                "field": "N° ID",
                "title": "N° ID",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Plate-forme",
                "title": "Plate-forme",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Date Msg",
                "title": "Date Msg",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Longitude",
                "title": "Longitude",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Latitude",
                "title": "Latitude",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Altitude",
                "title": "Altitude",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            },
            {
                "field": "Date Msg",
                "title": "Date Msg",
                "width": "",
                "align": "left",
                "formatter": "plaintext",
                "formatterParams": {
                    "target": "_blank"
                }
            }
        ],
        "outputs": 0,
        "cts": false,
        "x": 840,
        "y": 60,
        "wires": [],
        "info": "test"
    },
    {
        "id": "6681b08b.4a688",
        "type": "file in",
        "z": "67991373.45438c",
        "name": "Read File",
        "filename": "/home/pi/Documents/Python/CSV/ArgosData.csv",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 440,
        "y": 60,
        "wires": [
            [
                "e843fa29.885718"
            ]
        ]
    },
    {
        "id": "9a7a7971.dbc4b8",
        "type": "inject",
        "z": "67991373.45438c",
        "name": "",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 300,
        "y": 60,
        "wires": [
            [
                "6681b08b.4a688"
            ]
        ]
    },
    {
        "id": "e843fa29.885718",
        "type": "csv",
        "z": "67991373.45438c",
        "name": "",
        "sep": ",",
        "hdrin": true,
        "hdrout": "none",
        "multi": "mult",
        "ret": "\\r\\n",
        "temp": "",
        "skip": "0",
        "strings": true,
        "include_empty_strings": false,
        "include_null_values": false,
        "x": 590,
        "y": 60,
        "wires": [
            [
                "8476966506776ebb",
                "4440c71e.f88f18",
                "7b6e51f2bfdfb646"
            ]
        ]
    },
    {
        "id": "8476966506776ebb",
        "type": "debug",
        "z": "67991373.45438c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1030,
        "y": 60,
        "wires": []
    },
    {
        "id": "68524531a6a4f97b",
        "type": "comment",
        "z": "67991373.45438c",
        "name": "Problème : ",
        "info": "Impossible de mettre :\n\n`Date de loc.\nSat.\nQualité loc.`\n\nDans le tableau, raison probable = un point après le nom de l'objet qui fait buger lors du transfert du fichier CSV -> Tableau ",
        "x": 840,
        "y": 20,
        "wires": []
    },
    {
        "id": "7688e1b1256b496d",
        "type": "ui_worldmap",
        "z": "67991373.45438c",
        "group": "2f22af1f9a5f76b7",
        "order": 0,
        "width": 0,
        "height": 0,
        "name": "",
        "lat": "43.92962985967133",
        "lon": "2.1338488251496166",
        "zoom": "",
        "layer": "OSMG",
        "cluster": "",
        "maxage": "",
        "usermenu": "hide",
        "layers": "hide",
        "panit": "false",
        "panlock": "false",
        "zoomlock": "false",
        "hiderightclick": "true",
        "coords": "none",
        "showgrid": "false",
        "allowFileDrop": "false",
        "path": "/worldmap",
        "overlist": "DR,CO,RA,DN,HM",
        "maplist": "OSMG,OSMC,EsriC,EsriS,EsriT,EsriDG,UKOS",
        "mapname": "",
        "mapurl": "",
        "mapopt": "",
        "mapwms": false,
        "x": 1000,
        "y": 160,
        "wires": []
    },
    {
        "id": "7b6e51f2bfdfb646",
        "type": "function",
        "z": "67991373.45438c",
        "name": "",
        "func": "let values = msg.payload.length;\nx=0;\n\nwhile (x < values) {\nmsg.payload=\n{\n\"values=\":values,\n\"name\":\"TIPOD1\",\n\"label\":\"msg.payload.DateMsg\",\n\"lat\":msg.payload[x].Latitude,\n\"lon\":msg.payload[x].Longitude,\n\"icon\":\"https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lateral_buoy_r.svg/188px-Lateral_buoy_r.svg.png?20060717200653\"\n}\nreturn msg;\nx=x+1;\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 640,
        "y": 160,
        "wires": [
            [
                "e3d564c357cb9798"
            ]
        ]
    },
    {
        "id": "e3d564c357cb9798",
        "type": "debug",
        "z": "67991373.45438c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 830,
        "y": 160,
        "wires": []
    },
    {
        "id": "fe9b4de4.dcd72",
        "type": "ui_group",
        "name": "Data reçues ",
        "tab": "da612906.998368",
        "order": 1,
        "disp": true,
        "width": "25",
        "collapse": false
    },
    {
        "id": "2f22af1f9a5f76b7",
        "type": "ui_group",
        "name": "WorldMap",
        "tab": "b4d8e0b4b8e5de10",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "da612906.998368",
        "type": "ui_tab",
        "name": "Donnee",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    },
    {
        "id": "b4d8e0b4b8e5de10",
        "type": "ui_tab",
        "name": "World Map",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

image

ALSO: you are calling return msg so the loop never continues.


You need to store your array somewhere before altering msg.payload e.g...

const count = msg.payload.length;
const values = msg.payload;

for (let index = 0; index < values.length; index++) {
    const entry = values[index];
    msg.payload = {
        "values=": count,
        "name": "TIPOD1",
        "label": "msg.payload.DateMsg",
        "lat": entry.Latitude,
        "lon": entry.Longitude,
        "icon": "https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lateral_buoy_r.svg/188px-Lateral_buoy_r.svg.png?20060717200653"
    }
    node.send(msg);
}

PS, if you use monaco editor, you get a whole bunch of help in the function node.

your code in monaco...
image
... it lights up like a christmas tree.

Monaco is enabled in settings.js

1 Like

Nevermind, i found my answer, i didn't read the code corretly. Anyways, it's working fine now , thanks again and i wish you a good weekend!

Before i go, i got another small question too..

The parameter i need to use is "date msg" with the space between or it doesn't fit well with what the object send, but then the syntax is kinda broken, like in the example above, is there any way to fix that too?

You need to use bracket notation for variables with invalid characters in. E
G. entry["Date Msg"]

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