# 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'

**URL:** <https://discourse.nodered.org/t/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/79942>\
**Category:** General\
**Tags:** function-node\
**Created:** [19 July 2023 16:06 UTC](https://discourse.nodered.org/t/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/79942 "2023-07-19T16:06:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlabella](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mlabella](https://discourse.nodered.org/u/mlabella)\
**Post date:** [19 July 2023 16:06 UTC](https://discourse.nodered.org/t/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/79942/1 "2023-07-19T16:06:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tonymacdonald](https://avatars.discourse-cdn.com/v4/letter/t/77aa72/32.png) [@tonymacdonald](https://discourse.nodered.org/u/tonymacdonald)\
**Post date:** [19 July 2023 16:48 UTC](https://discourse.nodered.org/t/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/79942/2 "2023-07-19T16:48:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mlabella](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mlabella](https://discourse.nodered.org/u/mlabella)\
**Post date:** [19 July 2023 16:53 UTC](https://discourse.nodered.org/t/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/79942/3 "2023-07-19T16:53:44Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![tonymacdonald](https://avatars.discourse-cdn.com/v4/letter/t/77aa72/32.png) [@tonymacdonald](https://discourse.nodered.org/u/tonymacdonald)\
**Post date:** [19 July 2023 16:58 UTC](https://discourse.nodered.org/t/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/79942/4 "2023-07-19T16:58:23Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![mlabella](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mlabella](https://discourse.nodered.org/u/mlabella)\
**Post date:** [19 July 2023 17:01 UTC](https://discourse.nodered.org/t/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/79942/5 "2023-07-19T17:01:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tonymacdonald](https://avatars.discourse-cdn.com/v4/letter/t/77aa72/32.png) [@tonymacdonald](https://discourse.nodered.org/u/tonymacdonald)\
**Post date:** [19 July 2023 17:37 UTC](https://discourse.nodered.org/t/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/79942/6 "2023-07-19T17:37:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mlabella](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mlabella](https://discourse.nodered.org/u/mlabella)\
**Post date:** [19 July 2023 18:01 UTC](https://discourse.nodered.org/t/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/79942/7 "2023-07-19T18:01:03Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9fd41ef6ca21d10d392c611dd13d0c10eeddd4d7.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [19 July 2023 18:28 UTC](https://discourse.nodered.org/t/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/79942/8 "2023-07-19T18:28:42Z")

</div>

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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![mlabella](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mlabella](https://discourse.nodered.org/u/mlabella)\
**Post date:** [19 July 2023 18:31 UTC](https://discourse.nodered.org/t/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/79942/9 "2023-07-19T18:31:16Z")

</div>

I would like to send multiple emails.

---

<div class="post-metadata">

**Author:** ![mlabella](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@mlabella](https://discourse.nodered.org/u/mlabella)\
**Post date:** [19 July 2023 18:32 UTC](https://discourse.nodered.org/t/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/79942/10 "2023-07-19T18:32:57Z")

</div>

That worked! Thank you so much.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [19 July 2023 18:33 UTC](https://discourse.nodered.org/t/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/79942/11 "2023-07-19T18:33:44Z")

</div>

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.

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 August 2023 18:33 UTC](https://discourse.nodered.org/t/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/79942/12 "2023-08-02T18:33:46Z")

</div>

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