'Looping vue object in express/sql

I have a form that adds a project with x number of persons.

I'm using Nuxt with express and MySQL database.

I have a route method like this that inserts a project to my database:

app.post('/addNewProject', function (req, res) {
  var sql = "INSERT INTO project (name, startDate, endDate) VALUES ('"+req.body.projectName+"', '"+req.body.projectStart+"', '"+req.body.projectEnd+"'); INSERT INTO persons (projectID, name, workHours) VALUES (LAST_INSERT_ID(), '"+req.body.persons[0].personName+"', '"+req.body.persons[0].personHours+"')";
  connection.query(sql, function (err, rows) {
    if (err) throw err
    res.send(rows)
  })
})

The persons is a vue object persons: [{ personName: '', personHours: '' }], that can be filled in the form.

So since its persons[0] now, it only adds the first item to the database.

Im wondering if there is a way to loop the object inside this sql variable.

And I think I have to loop it here since I need the LAST_INSERT_ID() because I need to get the projectID that is a AUTO_INCREMENT primary key in the project table.

I have tried to use sql wihle loop but it doesn't seem to work here.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source