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

118
Views
mongodb user not being updated
export const editProfile = (req,res) => {
    
try{
    userModel.find({username:req.body.username},(err,user)=>{
       if (user.length !== 0) {
        if (typeof(req.body.nusername) === "undefined"){
            console.log("error")
        }else{
            const filter = {new:true}
         
            const opts = { new: true };
            console.log(filter)
           userModel.findOneAndUpdate({ username: req.body.username }, { $set: { username:req.body.nusername} }, opts)
        
            const token = jwt.sign({username:req.body.nusername,password:user.password,email:user.email,bio:user.bio,profileImg:user.profileImg},"Secret123")
         
           
                res.status(208).json({token:token})
        
            
        }
       }
    })

    
    
    

}catch(err){
    
}
}

its not updating the user in my mongodb database i dont know what im doing wrong is there anything in the code i am doing incorrect?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need use async await!

export const editProfile = (req,res) => {
    
try{
    await userModel.find({username:req.body.username},async (err,user)=>{
       if (user.length !== 0) {
        if (typeof(req.body.nusername) === "undefined"){
            console.log("error")
        }else{
            const filter = {new:true}
         
            const opts = { new: true };
            console.log(filter)
           await userModel.findOneAndUpdate({ username: req.body.username }, { $set: { username:req.body.nusername} }, opts)
        
            const token = jwt.sign({username:req.body.nusername,password:user.password,email:user.email,bio:user.bio,profileImg:user.profileImg},"Secret123")
         
           
                res.status(208).json({token:token})
        
            
        }
       }
    })

    
    
    

}catch(err){
    
}
}

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!