When i try to get promise back with save operation on instance of model. i get error: undefined is not a function
instance.save().exec().then(..)
However, if i try to get promise with model like this, then it works.
model.find(..).exec().then(..)
Is there no way to get promise for save action. Currently i just pass callback to save function. However, for the sake of consistency i'd like to do all db operations in the same manner.
Model#save
returns a promise, so you should skip the .exec()
:
instance.save().then(...);
Something like this?
let mongooseInstance = new MongooseInstance(Obj);
return mongooseInstance
.save()
.then(savedObj => {
if (savedObj) {
savedObj.someProperty = null;
success.data = savedObj;
return Promise.resolve(success);
} else {
return Promise.reject(error);
}
});
and maybe with a catch
?
mongooseInstance
.save()
.then(saved => console.log("saved", saved))
.catch(err => console.log("err while saving", err));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With