I am trying to call the function myFunction() and retrieve the source._id value, but I'm encountering issues with the current code. Even though source._id is correctly filled, I am unsure of how to successfully return it in its entirety. Ideally, something like:
var newId = myFunction();
Both the query and save operations are utilizing mongoose promises.
var myFunction = () => {
var query = MyModel.findOne({ user: userId, name: name.name });
query.exec((err, doc) => {
if (err) {
reject(err);
} else {
if (doc != null) {
var msg = "Error Msg here";
reject(new ValidationError(msg));
} else {
var source = new MyModel();
source.someUserProp = userId;
source.save((err, doc) => {
if (err) {
throw(err)
}
else {
return(source._id);
}
});
}
}
})
};