I am trying to get the object I inserted. I am referring to the example in the documentation. But I always get an "undefined". What am I doing wrong? Or is it no longer possible?
In my package.json is the following sqlite3 library "sqlite3": "^5.0.8"
.
My code (paste and give me the last inserted id)
sql = `INSERT INTO comment_hearts(post_id, comment_id, user_id, CREATED_AT)
VALUES (?,?,?,?)`
db.run(sql, [1, 100, 10, new Date().toString()], (err) => {
if (err) { console.log(err.message)};
console.log("THIS.lastID",this.lastID) // get always undefined
});
CodePudding user response:
You must pass a callback function with function () { ... }
for this.lastID
to not be undefined.
db.run(sql, [1, 100, 10, new Date().toString()], function(err) {
if (err) { console.log(err.message)};
console.log("THIS.lastID",this.lastID);
});
CodePudding user response:
Dont use arrow function. Pass as callback a function() {...}
. The reason is the context of this. The expression of an arrow function has no this of its own.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions