insertId shows as undefined


Im trying to print last insert Id but it always prints as undefined

Here is API code:'/post', (req, res) => {
  connection.query(`INSERT INTO tradeoffers (DCID, DCNAME, CATEGORY, IGN, ITEM1, Q1, ITEM2, Q2) VALUES ("${req.body.dcid}", "${req.body.dcname}", "${req.body.ign}", "${req.body.category}","${req.body.item1}", "${req.body.q1}", "${req.body.item2}", "${req.body.q2}");`, (err, result, rows) => {
    if (err) throw err
    else res.send(result)

Here is request code:

        'http://localhost:60/post', {
                    dcname: interaction.user.username + '#' + interaction.user.discriminator,
                    category: category,
                    ign: ign,
                    item1: iyh,
                    q1: quantityiyh,
                    item2: iyw,
                    q2: quantityiyw
                  .then(async function (response) {

                  .catch(function (error) {

When I print only response variable it shows right data:

but when I try to print only last inserted id it shows as undefined

>Solution :

The response variable contains a field called data which has the insertId in it, along with the other values you are passing via the API. You are currently specifying the wrong path to the variable you want.

Instead, use this:


Leave a ReplyCancel reply