mongoose await on save code example
Example 1: mongoose save method
var Tank = mongoose.model('Tank', yourSchema);
var small = new Tank({ size: 'small' });
small.save(function (err) {
if (err) return handleError(err);
// saved!
});
// or
Tank.create({ size: 'small' }, function (err, small) {
if (err) return handleError(err);
// saved!
});
// or, for inserting large batches of documents
Tank.insertMany([{ size: 'small' }], function(err) {
});
Example 2: mongoose await save
try {
const newUser = new User({
'email': req.body.email,
'name': req.body.name
});
console.log('before save');
let saveUser = await newUser.save(); //when fail its goes to catch
console.log(saveUser); //when success it print.
console.log('after save');
} catch (err) {
console.log('err' + err);
res.status(500).send(err);
}