In my Nodejs project I would just like to await
a mongoose.save function before continuing with my code. The below example does not work, but can anyone help me with something that will work please.
app.post('/api/CreateUser', async (req, res) => {
const newUser = new User({
'email': req.body.email,
'name': req.body.name
});
console.log('before save');
await newUser.save((err, userDoc) => {
if (err) return res.status(400).send(err);
console.log('saved item');
});
console.log('after save');
});
The current console.log order is:
- before save
- after save
- saved item
But I would like it to be:
- before save
- saved item
- after save
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…