This error will display in terminal:
TypeError: Cannot read property 'db' of undefined
And the error is still there even I add following code in router.js:
const mongo = require('koa-mongo');app.use(mongo({uri: 'mongodb://username:password@localhost:20002/blog',max: 100,min: 1}));
FYI, I can get correct response if I put all code in app.js
//router.js
router.get('/user', async (ctx, next) => {
ctx.body = await ctx.mongo.db('blog').collection('articles').find().toArray();
});
This error will display in terminal:
TypeError: Cannot read property 'db' of undefined
And the error is still there even I add following code in router.js:
const mongo = require('koa-mongo');
app.use(mongo({
uri: 'mongodb://username:password@localhost:20002/blog',
max: 100,
min: 1
}));
FYI, I can get correct response if I put all code in app.js