nswbmw / koa-mongo

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

ctx.mongo.db is not a function at /koa-mongo/index.js:65:24 #39

Closed mkgn closed 5 years ago

mkgn commented 5 years ago

I am on Koa2. My koa-mongo setup is;

    app.use(async (ctx, next) => {
        return next().catch((err) => { ... });
    })
    .use(mongo({
        uri: 'mongodb://localhost:27017/clicknwin',
        max: 10,
        min: 1
    }))     
    .use(helmet())
    .use(bodyParser())
    .use(router.routes());

The database exists can with the given uri, i can connect to mongo using compass. However when I run, it throws the below error.

TypeError: ctx.mongo.db is not a function at koaMongo (/usr/src/app/node_modules/koa-mongo/index.js:65:24)

I can't seem to find any issue in your code either (where you parse the db name). Anything abnormal you see in mine?

nswbmw commented 5 years ago

This bug was fixed at v1.8.0

mkgn commented 5 years ago

Now I get;

TimeoutError: ResourceRequest timed out at ResourceRequest._fireTimeout (/usr/src/app/node_modules/generic-pool/lib/ResourceRequest.js:62:17)

I did ;

npm update koa-mongo before building the app.

nswbmw commented 5 years ago

Make sure that your mongodb is started

mkgn commented 5 years ago

timeout doesn't make sense because if i try the same connection settings in mongo compass it works. i tried uri: mongodb://localhost:27017/clicknwin?authSource=none In Uri, i added authsource=none (which i think is not necessary) just to test since in the compass connection UI it's authsource is set to none.

I thought this may be a result of the change. Anyway i'll look further and let you know.

mkgn commented 5 years ago

Hi, Sorry for re-opening this again. Have you tried with latest Mongo 4.0, Node v10.15.2? Are you on a windows platform?

This sample : https://github.com/ceoworks/tutorial1-api-koa works fine, but it uses old mongo driver (2.2.10).

In your case, I updated the genericPool.createPool() and just added a console.log to catch() part and the error is; {"code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":27017}

i tried below connection strings: mongodb://127.0.0.1:27017/clicknwin mongodb://localhost:27017/clicknwin

Nothing works. But both works in that sample which is using the old driver. In my windows host file I have pointed 127.0.0.1 to localhost as well. also ::1 to localhost. So this shouldn't be a problem.

nswbmw commented 5 years ago

My environment:

Run this repo's example.js is ok.