Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

insertId shows as undefined

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

Here is API code:

app.post('/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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

                  })
                  .catch(function (error) {
                    console.log(error);
                  });

When I print only response variable it shows right data:
Log

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:

console.log(response.data.insertId)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading