Don't return promise if callback specified
See original GitHub issueHello, I’m the author of meanify, a library that uses Mongoose to cleverly generate API routes.
One of my users is bumping into an issue where he is seeing console.error output because I use callbacks to deal with Mongoose errors.
Bluebird appears to expect an explicit catch statement, forcing one to prefer one style to another i.e. promises vs callbacks upon installation.
Consider the following code:
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var User = mongoose.model('User', new mongoose.Schema({
name: { type: String, required: true },
}));
var user = new User();
user.save(function (err, doc) {
if (err) {
// error handled in the callback
console.error('Error:', err.message);
} else {
console.log('Success:', doc);
}
});
Results in this console output:
Error: User validation failed
Unhandled rejection ValidationError: User validation failed
at MongooseError.ValidationError (/Users/artz/Sites/meanify/node_modules/mongoose/lib/error/validation.js:23:11)
at model.Document.invalidate (/Users/artz/Sites/meanify/node_modules/mongoose/lib/document.js:1298:32)
at /Users/artz/Sites/meanify/node_modules/mongoose/lib/document.js:1173:16
at validate (/Users/artz/Sites/meanify/node_modules/mongoose/lib/schematype.js:662:7)
at /Users/artz/Sites/meanify/node_modules/mongoose/lib/schematype.js:693:9
at Array.forEach (native)
at SchemaString.SchemaType.doValidate (/Users/artz/Sites/meanify/node_modules/mongoose/lib/schematype.js:667:19)
at /Users/artz/Sites/meanify/node_modules/mongoose/lib/document.js:1171:9
at nextTickCallbackWith0Args (node.js:452:9)
at process._tickCallback (node.js:381:13)
at Function.Module.runMain (module.js:449:11)
at startup (node.js:139:18)
at node.js:999:3
My only solution to avoid this error for my users using Bluebird appears to be refactoring meanify
to use promises so there’s no error output. Any advice would be much appreciated.
Thanks!
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Aren't promises just callbacks? - Stack Overflow
Promises are not callbacks. A promise represents the future result of an asynchronous operation. Of course, writing them the way you do, you...
Read more >JavaScript Promise Tutorial – How to Resolve or Reject ...
First, let us create a generic function that accepts a PokeAPI URL as argument and returns a Promise. If the API call is...
Read more >Using promises - JavaScript - MDN Web Docs
Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function.
Read more >Promise#finally's callback doesn't accept Promise<void ...
The current typing, without the PromiseLike, suggests that the runtime will not wait for the onfinally callback to resolve if it's a Promise,...
Read more >Converting Callbacks to Promises in Node.js - Stack Abuse
The idea is to create a new Promise object that wraps around the callback function. If the callback function returns an error, we...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Right now mongoose always returns a promise, even if you specify a callback. I’d love to change this for a future release. However, not happening anytime soon because we’ve got more pressing issues. Here’s a couple alternatives:
.catch()
with an empty functionClosing this in favor of #3670, follow that issue for updates. Still planning on changing this for 5.0.