Function to take parsed CSV file and indicate where a '0' is and send an email with the name of the item that has the '0'

I am trying to write a function that will take data from a csv file where one column is filled with names and one with quantities and send the rows where the quantities are '0' out into an email.

tree,9
car,6
nuts,0
steel,2
track,2
screen,0
iron,3

This is an example of the csv before using the node-csv parser node. I have the email node configured correctly with the correct SMTP settings, but my function will not output the lines that say nuts and screen.

Have you tried sending the output of your function node to a debug node first to make sure that the function node is working?

What does the code in your function node currently do?

Yes, I have sent it out to a debug and even put in console.log's.

const rows = msg.payload;

for (let i = 0; i < rows.length; i++) {
    const row = rows[i];
    const itemName = row[1];
    const quantity = parseInt(row[2]);

    if (quantity == 0) {
        console.log(`Found item with quantity 0: ${itemName}`);
        msg.topic = 'Inventory Restock';
        msg.payload = `The item ${itemName} needs to be ordered.`;
        return msg;
    }
}
console.log('No items found with quantity 0.');
return null;

No dice as of right now.

I think your column indexes should be 0 and 1 rather than 1 and 2.
i.e.

const itemName = row[0];
const quantity = parseInt(row[1]);

I tried that. I just had another column for another value in the csv that was unnecessary for this question.

Can you post a sample of the input to your function node?

Here is the output of the csv parser node that will input to the function node.

You are outputting a single object per line from the csv node. But your function is for looping over an array.
For individual object messages, your function should be more like this.

const row = msg.payload;
const itemName = row.item_name;
const quantity = parseInt(row.quantity);
if (quantity == 0) {
    console.log(`Found item with quantity 0: ${itemName}`);
    msg.topic = 'Inventory Restock';
    msg.payload = `The item ${itemName} needs to be ordered.`;
    return msg;
}
console.log('No items found with quantity 0.');
return null;

Do you want to send multiple emails or one.

I would like to send multiple emails.

That worked! Thank you so much.

Then the function i posted should be fine.

I would probably not use a function just a switch and template node would do
e.g.

[{"id":"d1d56bb43a743647","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":280,"y":360,"wires":[["77fb41b21fbb9975"]]},{"id":"77fb41b21fbb9975","type":"template","z":"b9860b4b9de8c8da","name":"simulate csv file read","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"tree,9\ncar,6\nnuts,0\nsteel,2\ntrack,2\nscreen,0\niron,3\n","output":"str","x":460,"y":360,"wires":[["8d638e0563a7991d"]]},{"id":"8d638e0563a7991d","type":"csv","z":"b9860b4b9de8c8da","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"item_name,quantity","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":310,"y":420,"wires":[["64a45997f27221c6"]]},{"id":"64a45997f27221c6","type":"switch","z":"b9860b4b9de8c8da","name":"","property":"payload.quantity","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":450,"y":420,"wires":[["601e03e46004ad35"]]},{"id":"601e03e46004ad35","type":"template","z":"b9860b4b9de8c8da","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"The item {{payload.item_name}} needs to be ordered.","output":"str","x":580,"y":420,"wires":[["07a3c63a0cbcf370"]]},{"id":"07a3c63a0cbcf370","type":"debug","z":"b9860b4b9de8c8da","name":"debug 322","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":420,"wires":[]}]

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