Mongoose error on promise with save?

When I try to get a promise to return while maintaining the operation on the model instance. I get an error: undefined is not a function

 instance.save().exec().then(..) 

However, if I try to get a promise with such a model, then it works.

 model.find(..).exec().then(..) 

Unable to get a promise to keep the action. I am currently just passing a callback to save a function. However, for the sake of consistency, I would like to do all the db operations in the same way.

+8
mongoose
source share
2 answers

Model#save returns a promise, so you should skip .exec() :

 instance.save().then(...); 
+27
source share

Something like that?

 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 possibly with catch ?

 mongooseInstance .save() .then(saved => console.log("saved", saved)) .catch(err => console.log("err while saving", err)); 
+2
source share

All Articles