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:
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
CodePudding user response:
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)