How am I working with nodejs, mysql, forEach trying to save the email in my BD with a unique id, using the textarea tag, based on this question
Insert multiple email to mysql using single textarea
For-each over an array in JavaScript
javascript split on non-alphanumeric an keep delemiters at start
<form class="" action="/registration/instudent/{{id_school}}/{{mat}}/{{grade}}" method="post">
<textarea name="email" ></textarea>
<button class="btn btn-lg">Save</button>
</form>
Using the example in PHP base, in my file example.js I have this code, try to save only the email value of my textarea tag.
router.post('/instudent/:id_school/:mat/:grade', isLoggedIn, async (req,res) => {
const { id_school, mat, grade } = req.params;
const { email } = req.body;
const e = email.match('\r\n');
//var e = ["a@gmail.com", "b@gmail.com", "c@gmail.com"];
//const uStudent = {
// id_school,
// mat,
// grade,
// email
// };
e.forEach(function(email, indice, array) {
// await db.query('INSERT INTO date set ?', [uStudent]);
db.query("INSERT INTO date (email) VALUES (email)");
console.log('this is my email:',email);
});
//res.redirect('/');
});
If I try to save all the variables by executing the loop asynchronously, I find the following error.
const uStudent = {
id_school,
mat,
grade,
email
};
await db.query('INSERT INTO date set ?', [uStudent]);
UnhandledPromiseRejectionWarning: TypeError: uStudent.forEach is not a function
If I insert two emails this way.
db.query("INSERT INTO date (email) VALUES (email)");
In my database I only see the blank fields, someone can help me or give me a guide to know what I am doing wrong.