Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
NodeJs SQLITE Insert and get the inserted object or last inserted id

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
}); 
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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);
}); 
about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!