Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda