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

155
Views
POST one data value in 2 models Json, mongoDB, MongoDB

I created a code that has 2 functions;

module.exports.registerAccount = (reqBody) => {

let newAccount = new User({
    firstName : reqBody.firstName,
    lastName : reqBody.lastName,
    email : reqBody.email,
    mobileNum : reqBody.mobileNum,
    password : bcrypt.hashSync(reqBody.password, 10)
})

return await newAccount.save().then((account, error) =>{
    if(error){
        return false;
    }
    else{
        return true;
    }
})

let newCustomer = new Order ({
    FirstName : reqBody.firstName,
    LastName : reqBody.lastName,
    MobileNum : reqBody.mobileNum
})

return await newCustomer.save().then((customer, error) =>{
    if(error){
        return false;
    }
    else{
        return true;
    }
})
}

The newAccount is for the user model and the newCustomer is for the order model, I tried the codes and there's no error but the newCustomer is not saving in its model. I also tried to swap the position of the two model and the result is also swap. is there a way to make both of it work in one go? can I have some tips?

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

0

I found the solution;

return newAccount.save() && newCustomer.save().then((account, error) =>{
    if(error){
        return false;
    }
    else{
        return true;
    }
})

since that only one return statement is working I used && in between newAccount and newCustomer.

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!