0

I'm using nodeJS and mysql to insert a few rows in my database. Here is my code:

 connection.query('INSERT INTO trainings (file_id, selection_start, selection_end, label_id) VALUES ?', [toInsert], function(err, results){ ... } 

As you can see I'm inserting several rows at a time. How can I get all the inserted id if my table is in autoIncrement?

results.insertId only returns one ID (and not all)

Any idea on how I could fix that?

1
  • if answer correct you should up vote it Commented Nov 18, 2017 at 21:31

1 Answer 1

1

put results.insertId values into an array

var lastIds = []; /* you insert data method callback with model */ model.insertDataRow(request.body,function(err,results){ lastIds.push(results.insertId) }) 
Sign up to request clarification or add additional context in comments.

2 Comments

I might be wrong but it's not doing all the inserts at the same time (how can I know all the rows have been inserted?). My callback function is only called once (I assume when all the rows have been inserted)
like: connection.query('INSERT INTO posts SET ?', {title: 'test'}, function(err, result) { if (err) throw err; console.log(result.insertId); }); put result.insetId into array and return to fetch that... hope it might be work. reference: github.com/felixge/node-mysql/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.