Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.save() throws expection when using with co / koajs #2371

Closed
michelalbers opened this issue Oct 12, 2014 · 5 comments
Closed

.save() throws expection when using with co / koajs #2371

michelalbers opened this issue Oct 12, 2014 · 5 comments
Milestone

Comments

@michelalbers
Copy link

Hi,

I use the latest master release in my koajs project and I encountered some strange issue:

try {
  userToSave = new User(body.user);
  (yield userToSave.save());
  return this.body = [userToSave._id];
} catch (_error) {
  e = _error;
  return this["throw"](500, e);
}

In my tests im trying to actively save a User which will not pass validation. Mongoose then throws an error: No listeners detected, throwing. Consider adding an error listener to your connection.

Even though I wrapped my JavaScript yield userToSave.save() statement in try-catch block the exception cannot be caught and therefore crashes my application 👎

Isn't that contradictionary to the promise pattern? Shouldn't it simply call Promise.reject and not throw an exception on it's own?

The part in mongoose causing this (lib/document.js:1433):

Document.prototype.$__handleReject = function handleReject(err) {
  // emit on the Model if listening
  if (this.listeners('error').length) {
    this.emit('error', err);
  } else if (this.constructor.listeners && this.constructor.listeners('error').length) {
    this.constructor.emit('error', err);
  } else if (this.listeners && this.listeners('error').length) {
    this.emit('error', err);
  } else if (this.db) {
    // emit on the connection
    if (!this.db.listeners('error').length) {
      err.stack = 'No listeners detected, throwing. Consider adding an error listener to your connection.\n' + err.stack
    }
    this.db.emit('error', err);
  } else {
    throw err;
  }
}

Update: Even when using the classic callback approach, the exceptions gets thrown. That can't be right or can it be?

Best,
Michel

@vkarpov15 vkarpov15 added this to the 4.0 milestone Oct 17, 2014
@vkarpov15
Copy link
Collaborator

Thanks for pointing this out, I'll investigate.

@vizo
Copy link

vizo commented Oct 28, 2014

I have same issue .... with normal callback from save ....

No listeners detected, throwing. Consider adding an error listener to your connection.
ValidationError: Path firstName is required.
at model.Document.invalidate (/node_modules/mongoose/lib/document.js:1040:32)
at /node_modules/mongoose/lib/document.js:985:16
at validate (/node_modules/mongoose/lib/schematype.js:632:7)
at /node_modules/mongoose/lib/schematype.js:653:9
at Array.forEach (native)
at SchemaString.SchemaType.doValidate (/node_modules/mongoose/lib/schematype.js:636:19)
at /node_modules/mongoose/lib/document.js:983:9
at process._tickCallback (node.js:419:13)

@vkarpov15 vkarpov15 modified the milestones: 3.9.6, 4.0 Nov 10, 2014
@vkarpov15 vkarpov15 modified the milestones: 4.0, 3.9.6 Dec 5, 2014
@justin-lau
Copy link

I am able to reproduce this issue in v3.9.7 mongoose test on the ES6 validate() method only:

$ MONGOOSE_DISABLE_STABILITY_WARNING=1 ./node_modules/.bin/mocha --harmony --grep "Documents in ES6 validate()" test/harmony

The test would fail with noErrorListener turned on:

db = start({ noErrorListener: 1 });

However, if I add a model level error listener to the test case that does nothing, the test passes:

M.on('error', function(){});

@vkarpov15 vkarpov15 modified the milestones: 4.0.0-rc0, 4.0 Dec 31, 2014
@joethaw
Copy link

joethaw commented Jan 16, 2015

I also encounter this issue where the exception still get past the try catch around save() function and crash the process. Like justin-lau mentioned above, the model level error listener does not catch that exception. The exception in my case was thrown in my custom validation function.

@vkarpov15
Copy link
Collaborator

Thanks @justin-lau, debugged it for me :) looks like its tricky to determine if there are listeners or not when using yield, so I'm just gonna get rid of that exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants