Categories
JavaScript Answers

How to retrieve last inserted id with Node MySQL?

Spread the love

To retrieve last inserted id with Node MySQL, we use the insertedId property.

For instance, we write

connection.query(
  "INSERT INTO posts SET ?",
  { title: "test" },
  (err, result, fields) => {
    if (err) throw err;

    console.log(result.insertId);
  }
);

to call query with a callback that gets the result.insertId property to get the ID of the last inserted item.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *