1

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.

sheila.ars
  • 57
  • 1
  • 7
  • Have you tried `db.query('INSERT INTO date set ?', uStudent)`? – tadman Jan 27 '20 at 19:23
  • Yes, the result is the same, I throw the following error: (node:39926) UnhandledPromiseRejectionWarning: TypeError: uStudent.forEach is not a function – sheila.ars Jan 27 '20 at 19:27
  • If this is the [`mysql` driver on NPM](https://www.npmjs.com/package/mysql), then you appear to be doing it correctly. Are you sure it's emitted by that line? Personally I prefer to use [Sequelize](https://sequelize.org/v5/), but the approach is similar. – tadman Jan 27 '20 at 19:29

1 Answers1

0

Thank you very much for the help, it's correct you told me tadman correctly inserted in this way to my database.

db.query('INSERT INTO date set ?', uStudent)

The correct way I managed to insert data is to help me in this question

Why is “forEach not a function” for this object?

Then my code would look like this, I hope it helps, since I've been trying to save in database with forEach for several days.

router.post('/instudent/:id_school/:mat/:grade', isLoggedIn, async (req,res) => {
  const { id_school, mat, grade } = req.params;
  const { email } = req.body;

  var uStudent = {
    id_school,
    mat,
    grade
}

Object.keys(uStudent).forEach(function (key){
    console.log(uStudent[key]);
    db.query('INSERT INTO date set ?', uStudent);
});
  //res.redirect('/');
});
sheila.ars
  • 57
  • 1
  • 7