nswbmw / koa-mongo

MongoDB middleware for koa, support connection pool.
145 stars 31 forks source link

How can I use with koa-router? #20

Closed HenryYong closed 5 years ago

HenryYong commented 7 years ago

//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

nswbmw commented 7 years ago

put app.use(mongo({...})) before router