Mongoose: Async/Await doesn't work with a query, nothing happens, execution freezes

Created on 14 Jul 2017  路  5Comments  路  Source: Automattic/mongoose

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

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.

All 5 comments

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:

  1. the mongoose global option e.g. mongoose.set('bufferCommands', false);
    using the global option will effect all connections/models
  2. the connection option e.g. mongoose.connect(URI, { bufferCommands: false });
    using the connection option will effect all models that use this connection
  3. the schema option e.g. mySchema.set('bufferCommands', false)
    using the schema option will only effect the model(s) compiled from this schema

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:

  1. the mongoose global option e.g. mongoose.set('bufferCommands', false);
    _using the global option will effect all connections/models_
  2. the connection option e.g. mongoose.connect(URI, { bufferCommands: false });
    _using the connection option will effect all models that use this connection_
  3. 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

Was this page helpful?
0 / 5 - 0 ratings