Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda