Define a function within a function and pass an object, forEach

I have an array in a flow variable like this:

I want to cycle through each of the objects in the array, and send out a new message for each one, using some values from the objects to build the new message.

let machines = flow.get("machines") || [];

var iterate = function(id, name) {
    node.warn(id);
    node.warn(name);
    var url = "http://example.com/" + id + "/" + name + "/api";
    msg.url = url;
    node.send(msg.url);
};
machines.forEach(iterate(msg.id, msg.name));

I'm having trouble passing the array in to the function in this way. Can anyone offer hints on this process?

machines.forEach((arr) => {
    iterate(arr[0].id, arr[0].name)
});

If i read your payload image correctly.

[edit] edit fixed typo.

thank you, here's where I'm at now:

let machines = flow.get("machines") || [];
node.warn(machines);

var iterate = function(id, name) {
    node.warn(id);
    node.warn(name);
    var url = "http://example.com/" + id + "/" + name + "/api";
    msg.url = url;
    node.warn(msg.url);
};

machines.forEach((arr) => {
    iterate(arr[0].id, arr[0].name)
});

return msg;

I only get the array out of the first node.warn, so it would appear nothing is making it into the iterate function:

Show us machines array.
I would also declare your function with const not var, and use let inside your function, rather than var.
[edit]
The first post showed an array of single arrays containing an object,
Your new image shows an array of objects.
With and array of objects it would be.

let machines = flow.get("machines") || [];
// machines =[{id:1,name:"one"},{id:2,name:"two"}];
node.warn(machines);

const iterate = function(id, name) {
    node.warn(id);
    node.warn(name);
    let url = "http://example.com/" + id + "/" + name + "/api";
    msg.url = url;
    node.warn(msg.url);
};

machines.forEach((obj) => {
    iterate(obj.id, obj.name)
});

return msg;

the array is in the image I just posted in my last reply, here is is in the flow context storage:

I take your advice of the const / let... but I don't pretend to know why!

Which is not the same as your original image, the goal posts moved.

that's odd. I didn't do anything to change that...

lovely... this works, thank you. In one example you used 'arr' and in the next you used 'obj'. Am I correct that you could have used 'foo', or anything, but you are using those specifically just for code clarity?

It's a naming convention as the first foreEach was creating an array for each loop so I named it arr,. The second example looped through and was producing an object so I named it obj. That way you know later when you look back or bug fix, you know you expected an array or object.

so any idea why the data changed formats like that? This comes straight from the API response... so I can only guess they made an API change on me?

There could be a object reference issue as forech works on each item and the context is referenced to machines. But I'm not seeing why msgid dissappeared and serial remained. If it is an issue you could clone machines using RED.utils.cloneMessage(machines).ForEach(...

call the api and show us what it returns maybe they changed their output.

Are others working on this machine?

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