3

This is the code of route. When I use commented Promise, it returns 123 in body. But with mongoose query it returns 404 status. Item in log founds good. But it seems router just ignore await and return 404 immediately. What am I doing wrong?

router.get('/:id', async (ctx, next) => {
    // var item = await Promise.resolve(123); // this line works good!
    var item = await Model.findById(ctx.params.id); // but this not
    ctx.body = item;
    console.log('hmm', item, ctx.response);
});

In console.log output everything good, but throws 404 Not Found like default koa response:

hmm { _id: 5accda0700c0afd3ca50bc67,
  name: 'yuki 2',
  server: 5accd5848ae2e2d2be1760c6,
  owner: 5accd023cc3a90d1f73d4afd,
  createdAt: 2018-04-10T15:36:39.965Z,
  updatedAt: 2018-04-10T15:36:39.965Z,
  __v: 0 } 
 { status: 404,
  message: 'Not Found',
  header: 
   { 'access-control-allow-credentials': 'true',
     'content-type': 'text/plain; charset=utf-8',
     'content-length': '9' },
  body: { _id: 5accda0700c0afd3ca50bc67,
     name: 'yuki 2',
     server: 5accd5848ae2e2d2be1760c6,
     owner: 5accd023cc3a90d1f73d4afd,
     createdAt: 2018-04-10T15:36:39.965Z,
     updatedAt: 2018-04-10T15:36:39.965Z,
     __v: 0 } }
Disorder
  • 167
  • 2
  • 13

1 Answers1

-1

It may be because of mongoose promise being deprecated, after the line where you require mongoose library, eg:

const mongoose = require('mongoose');

You can add one line

mongoose.Promise = global.Promise;

or

mongoose.Promise = require('bluebird');

If you are using bluebird in your project.

Akshay Gohil
  • 370
  • 2
  • 10