question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Don't return promise if callback specified

See original GitHub issue

Hello, 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:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
vkarpov15commented, Mar 3, 2016

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:

  1. Don’t use bluebird. IMO it’s a bloated mess that’s more trouble than it’s worth
  2. Use .catch() with an empty function
user.save(function (err, doc) {
  if (err) {
    // error handled in the callback
    console.error('Error:', err.message);
  } else {
    console.log('Success:', doc);
  }
}).catch(function() {});
0reactions
vkarpov15commented, Dec 10, 2017

Closing this in favor of #3670, follow that issue for updates. Still planning on changing this for 5.0.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found