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

MongooseError thrown in process.nextTick crashes process #6767

Closed
lkho opened this issue Jul 25, 2018 · 1 comment
Closed

MongooseError thrown in process.nextTick crashes process #6767

lkho opened this issue Jul 25, 2018 · 1 comment
Milestone

Comments

@lkho
Copy link
Contributor

lkho commented Jul 25, 2018

I want to report a bug

What is the current behavior?

let A = mongoose.model('a', new Schema({}));
await A.find().populate('id'); // `id` is virtual getter of `_id`
// throws MongooseError: If you are populating a virtual, you must....

The error is expected. However, the error is thrown in nextTick instead of rejecting the promise, and the process crashed.

C:\...\node_modules\mongoose\lib\utils.js:226
        throw error;
        ^
MongooseError: If you are populating a virtual, you must set the localField and foreignField options
    at new MongooseError (C:\...\node_modules\mongoose\lib\error\mongooseError.js:11:11)
    at getModelsMapForPopulate (C:\...\node_modules\mongoose\lib\model.js:3947:13)
    at populate (C:\...\node_modules\mongoose\lib\model.js:3486:21)
    at _populate (C:\...\node_modules\mongoose\lib\model.js:3456:5)
    at utils.promiseOrCallback.cb (C:\...\node_modules\mongoose\lib\model.js:3429:5)
    at Object.promiseOrCallback (C:\...\node_modules\mongoose\lib\utils.js:223:14)
    at Function.Model.populate (C:\...\node_modules\mongoose\lib\model.js:3428:16)
    at cb (C:\...\node_modules\mongoose\lib\query.js:1606:17)
    at result (C:\...\node_modules\mongodb\lib\utils.js:414:17)
    at executeCallback (C:\...\node_modules\mongodb\lib\utils.js:406:9)
    at handleCallback (C:\...\node_modules\mongodb\lib\utils.js:128:55)
    at cursor.close (C:\...\node_modules\mongodb\lib\operations\cursor_ops.js:218:62)
    at handleCallback (C:\...\node_modules\mongodb\lib\utils.js:128:55)
    at completeClose (C:\...\node_modules\mongodb\lib\cursor.js:887:14)
    at _endSession (C:\...\node_modules\mongodb\lib\cursor.js:898:37)
    at Cursor._endSession (C:\...\node_modules\mongodb-core\lib\cursor.js:193:5)
    at Cursor._endSession (C:\...\node_modules\mongodb\lib\cursor.js:226:59)
    at Cursor.close (C:\...\node_modules\mongodb\lib\cursor.js:898:19)
    at cursor._next (C:\...\node_modules\mongodb\lib\operations\cursor_ops.js:218:23)
    at handleCallback (C:\...\node_modules\mongodb-core\lib\cursor.js:202:5)
    at _setCursorNotifiedImpl (C:\...\node_modules\mongodb-core\lib\cursor.js:560:38)
    at self._endSession (C:\...\node_modules\mongodb-core\lib\cursor.js:568:46)

Process finished with exit code 1

mongoose/lib/utils.js

Lines 220 to 229 in 27cc9e7

exports.promiseOrCallback = function promiseOrCallback(callback, fn) {
if (typeof callback === 'function') {
try {
return fn(callback);
} catch (error) {
return process.nextTick(() => {
throw error;
});
}
}

If the current behavior is a bug, please provide the steps to reproduce.

Just populate any virtual field without the localField and foreignField options will throw.

What is the expected behavior?
The error should either returned in the callback, or reject the promise, but not crashing the process.

Please mention your node.js, mongoose and MongoDB version.
node 8.10.0
mongoose 5.2.5
MongoDB 3.6.1 Community

@vkarpov15 vkarpov15 modified the milestones: 5.2.7, 5.2.6 Jul 30, 2018
@vkarpov15
Copy link
Collaborator

Thanks for reporting, will fix ASAP

vkarpov15 added a commit that referenced this issue Jul 30, 2018
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

2 participants