Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

226
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda