I am building a website contact form. I have form submissions being added to Firebase via Angular js. When a new submission is submitted to Firebase I am using child\_added in Node to trigger nodemailer. This is working fine, except that the entire submissions collection is being re-emailed whenever I restart the dev server, and multiple times daily in production on Heroku. How can I ensure that emails won't be sent more than once?

```
var myRoot = new Firebase('https://firebase.firebaseio.com/website/submissions');

myRoot.on('child_added', function(snapshot) {
    var userData = snapshot.val();
    var smtpTransport = nodemailer.createTransport("SMTP",{
        auth: {
            user: "email@gmail.com",
            pass: "password"
        }
    });

var mailOptions = {
    from: "Website <email@gmail.com>", // sender address
    to: "email@email.com.au", // list of receivers
    subject: "New  Website Lead", // Subject line
    html: "<p><strong>Name: </strong>" + userData.name + "</p>" + "<p><strong>Email: </strong>" + userData.email + "</p>" + "<p><strong>Phone: </strong>" + userData.phone + "</p>" + "<p><strong>Enquiry: </strong>" + userData.enquiry + "</p>" + "<p><strong>Submitted: </strong>" + userData.date + "</p>" // html body
};

smtpTransport.sendMail(mailOptions, function(error, response) {
    if(error) {
        console.log(error);
    }
    else {
        console.log("Message sent: " + response.message);
    }
    smtpTransport.close();
    });
});

```
['https://Stackoverflow.com/questions/19719635', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2934691/']
You could use

```
var duty = [
        "Operate Equipment",
        "Check for Water Damage",
        "Check for Mold",
        "Follow Insurance Procedure",
        "Restore"
    ],  
    x = parseInt( document.getElementById('counter').value, 10 );
document.getElementById('task').innerHTML = duty.slice(0, x).join('<br />');

```

---

**Notes**

* Always use the second argument (radix) when using `parseInt` or `parseFloat`.
* If `document.getElementById('counter').value` is a numerical string (like `'3'`), don't use `parseInt`. Instead, if necessary, use `+'3'` to get the number `3`.

Only use `parseInt` if you want to parse something like `'3px'` into `3`.
* Don't use `getElementById` to get the same element at each iteration of a loop.

Then, instead of

```
for(var i = 0; i < n; ++i) {
   doSomething(document.getElementById('foo'), i);
}

```

use

```
var el = document.getElementById('foo');
for(var i = 0; i < n; ++i) {
   doSomething(el, i);
}

```
* Never change `innerHTML` at each iteration of a loop, because modifying the DOM has bad performance.

Then, instead of

```
for(var i = 0; i < n; ++i) {
   element.innerHTML += someHTML(i); // n DOM modifications
}

```

use

```
var html = '';
for(var i = 0; i < n; ++i) {
   html += someHTML(i);
}
element.innerHTML = html; // only 1 DOM modification

```
```
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test2</title>
</head>
<body>
    <input type="button" onclick="dutyarray()" value="Click">To see my top
    <input type="number" id="counter"> job duties here</input>
    <br />
    <p id="task">Duties</p>
    <script>
        function dutyarray() {
            var x = document.getElementById('counter').value;
            x = parseInt(x);

            var duty =
                [
                "Operate Equipment",
                "Check for Water Damage",
                "Check for Mold",
                "Follow Insurance Procedure",
                "Restore"
                ];

            if (x > duty.length-1) x = duty.length-1;   // Performs some checking

            document.getElementById('task').innerHTML = "";
            for ( ; x >= 0; x--) {
                document.getElementById('task').innerHTML += duty[x] + "<br>";
            }
        }
    </script>
</body>
</html>

```