Do you want to request a feature or report a bug?
report a bug
What is the current behavior?
I'm trying to use Mongoose with async/await, but the query never finishes
If the current behavior is a bug, please provide the steps to reproduce.
this is a sample endpoint from my Koa app. The problem is the code "freezes" at the try catch block and no error is even caught
router.post('/auth', async function(ctx, next) {
var data = ctx.request.body || {};
console.log('fetching user')
try {
var user = await User.findOne({ email: data.email }).exec();
} catch(err) {
console.log('error caught');
console.log(err);
}
//it never reaches this point
console.log(user);
console.log('finished');
ctx.body = 'test';
await next();
});
The stdout for this will just end at fetching user, the code execution literally freezes there.
I tried it with default mpromise library and then with native promises.
What is the expected behavior?
Something should happen, either get the user or catch error
Please mention your node.js, mongoose and MongoDB version.
Node 8.1.4, Mongoose 4.11.2, MongoDB 3.4.6
How are you connecting to the db? Are you using connect or createConnection?
I'm sorry for this, the thing is I was rewriting the whole server and forgot to connect to database. Some kind of error should be thrown in this case though.
face
I'm sorry for this, the thing is I was rewriting the whole server and forgot to connect to database. Some kind of error should be thrown in this case though.
haha
face the same issue
I think just about everyone runs into this at some point, at least that's what I tell myself every time I do it :smile:
There is an entry on the FAQ here that points to the docs on operation buffering. By default mongoose will buffer Model operations until the connection is established. You can disable this behavior by setting bufferCommands
to false.
This can be achieved in the following ways:
mongoose.set('bufferCommands', false);
mongoose.connect(URI, { bufferCommands: false });
mySchema.set('bufferCommands', false)
I think just about everyone runs into this at some point, at least that's what I tell myself every time I do it 馃槃
There is an entry on the FAQ here that points to the docs on operation buffering. By default mongoose will buffer Model operations until the connection is established. You can disable this behavior by setting
bufferCommands
to false.This can be achieved in the following ways:
- the mongoose global option e.g.
mongoose.set('bufferCommands', false);
_using the global option will effect all connections/models_- the connection option e.g.
mongoose.connect(URI, { bufferCommands: false });
_using the connection option will effect all models that use this connection_- the schema option e.g.
mySchema.set('bufferCommands', false)
_using the schema option will only effect the model(s) compiled from this schema_
I just want to thank you buddy
Most helpful comment
I'm sorry for this, the thing is I was rewriting the whole server and forgot to connect to database. Some kind of error should be thrown in this case though.