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

229
Views
lastID is undefined using sqlite3 in Node.js

Database inserted successfully but this.lastID is undefined. Can anyone help me what is the wrong with this code?

app.post('/products/create', (req,res) => {
    
    upload(req, res, (err) => {
        const name = req.body.name
        const description = req.body.description
        const Filename = req.file.filename
        const id = this.lastID
        console.log(id)
        //console.log('Selected Files: ' + req.Filename);
        db.uploadPost(name, description, Filename, function(error){
            if(error){
                console.log(err)
            }
            else{
                console.log(this.lastID)
                res.redirect('/products/'+id)
            }
        })
    })
})

and my databse

exports.uploadPost = function(name, description, Filename, callback){
        const query = "INSERT INTO products('name', 'description', 'image') VALUES (?,?,?)"
        const values =  [name, description, Filename]
        db.run(query,values, function(error){
            callback(error)
        })
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In the context of upload method, the property this.lastID is not available because you execute the insertion query at a latter stage.

One solution would be the following:

exports.uploadPost = function(name, description, Filename, callback){
    const query = "INSERT INTO products('name', 'description', 'image') VALUES (?,?,?)"
    const values =  [name, description, Filename]
    db.run(query,values, function(error){
        callback(error, this.lastID) // pass the computed lastID as a parameter of the callback
    })
}

And then change:

app.post('/products/create', (req,res) => {

    upload(req, res, (err) => {
        const name = req.body.name
        const description = req.body.description
        const Filename = req.file.filename
    
        db.uploadPost(name, description, Filename, function(error, id){
            if(error){
                console.log(err)
            }
            else{
                // use the passed id here, after the insertion query has been executed successfully 
                console.log(id)
                res.redirect('/products/'+id)
            }
        })
    })
})
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!