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

185
Views
nested mysql query get skipped in nodejs

I'm' trying to create a 'groupe' and then insert its creator as administrator in 'groupemembers' table, however the second query gets skipped

router.post('/createGroupe/:userId', upload.single('file'), (req, res) => {
    let groupe = req.body
    // req.body containing the groupe title, description
    let userId = req.params.userId
    let groupeId
    groupe['image'] = req.file
    db.query('insert into groupes set ?', groupe, function(err, result){
        if (err) throw err;
        groupeId = result.insertId.toString()
        db.query("insert into groupemembers set ?", [groupeId, userId, 'admin'], function (err, result){
            console.log(groupeId)
            if (err) return err;
        })
        res.send(result.insertId.toString())
    })
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you need to learn callback style or even better async/await style. much more easier to code.

but for your specific concern, i think you wanted to put the res.send one line up because that way you will call res.send AFTER the second query has executed.

router.post('/createGroupe/:userId', upload.single('file'), (req, res) => {
    let groupe = req.body
    // req.body containing the groupe title, description
    let userId = req.params.userId
    let groupeId
    groupe['image'] = req.file
    db.query('insert into groupes set ?', groupe, function(err, result){
        if (err) throw err;
        groupeId = result.insertId.toString()
        db.query("insert into groupemembers set ?", [groupeId, userId, 'admin'], function (err, result){
            console.log(groupeId)
            if (err) return err;
            res.send(result.insertId.toString())
        })
    })
})

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!