Get strings to new array

Hello, i tried to get product names out of an object array in a new array in a function node but i get an error and i do not know how to get it working.
Can someone help me?

function node:
var abgelaufen = {};
var abgelaufenname = ;

abgelaufen.laenge = msg.payload.overdue_products.length;
for (var idx=0; idx<=abgelaufen.laenge; idx++){
abgelaufenname.push({payload:msg.payload.overdue_products[idx].product.name});
}
msg.payload = abgelaufenname;
return [msg,abgelaufen];

error:
"TypeError: Cannot read property 'product' of undefined"

Thanks to all in advance.

Just at a quick glance, You are doing idx <= length. But the length is 1 longer than index, as idx starts at 0. try idx < length.

That should probably be
return msg

If posting code in the future please use the technique described in this post so the forum does not mess with text. How to share code or flow json

1 Like

Thanks, now i get an output. But only for the first element in the array. The other ones are lost.

Show us what you have in the function now

I have no idea, as I have no idea what your input object is, or what you wish to be returned.

var abgelaufenname = [];
var abgelaufen = {};
var idx = 0;

abgelaufen.laenge = msg.payload.overdue_products.length;
for (idx=0; idx<abgelaufen.laenge; idx++){
    abgelaufenname.push({payload:msg.payload.overdue_products[idx].product.name});
}
msg.payload = abgelaufenname;
return msg;```

Sorry, my fault.
I changed

msg.payload = abgelaufenname;

to

msg.payload = abgelaufenname;

Now everything is output with msg.payload.payload 
msg.payload would be better but i can work with this also.

If I understand what you mean that is because you have
abgelaufenname.push({payload:msg.payload.overdue_products[idx].product.name})

so you are pushing objects to the array. If you just want the values then try
abgelaufenname.push(msg.payload.overdue_products[idx].product.name)
which will push just the values.

Thank you very much!
Everything is working now with msg.payload
Just for Information what i am doing.
I tried to get an daily overview/alert via telegram of my overdue food products via restapi from my grocy erp from here grocy - ERP fĂĽr deinen KĂĽhlschrank
Everything is working now as expected.
Thanks to all!
:slightly_smiling_face: :+1:

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