Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'collection' of undefined with RateLimiterMongo #26

Closed
tero opened this issue May 21, 2019 · 6 comments · Fixed by #27
Closed

Cannot read property 'collection' of undefined with RateLimiterMongo #26

tero opened this issue May 21, 2019 · 6 comments · Fixed by #27

Comments

@tero
Copy link
Contributor

tero commented May 21, 2019

I'm implementing rate limit functionality with RateLimiterMongo and _initCollection function throws TypeError: Cannot read property 'collection' of undefined. Changing this.client.db to this.client from rows 80 and 81 will fix the issue.

This is with MongoDB version 3.4.10.

@animir
Copy link
Owner

animir commented May 21, 2019

@tero Hi, could you paste rate limiter config here and which nodejs library you use for MongoDB?

@tero
Copy link
Contributor Author

tero commented May 21, 2019

For example this will fail. models is our internal package and models.connection is mongoose.connection.

const { RateLimiterMongo } = require('rate-limiter-flexible')
const models = require('models')

const rateLimiter = new RateLimiterMongo({
  storeClient: models.connection,
  tableName: 'rate-limits',
  points: 3, // 3 requests
  duration: 60, // per 60 seconds by IP
})

I think the problem is that this is executed immediately when module is loaded by require and connection is not established yet. mongoose.connection will pool the requests but mongoose.connection.db is not available.

@animir
Copy link
Owner

animir commented May 22, 2019

@tero Is it correct, that you use models.connection before you have established connection with mongoose? If so, what is a reason to postpone?

@animir
Copy link
Owner

animir commented May 23, 2019

@tero Which version of mongoose do you use?

@tero
Copy link
Contributor Author

tero commented May 23, 2019

I am using Mongoose 4.13.17.

How we establish and use connection in our project is quite simple and that relies on mongoose's ability to buffer requests:

const express = require('express')
const models = require('models')
// Require routes and that means also rate limiter object is initialized.
// Because collection and indexes are created on init it will query database now
const routes = require('./routes')
const app = express()

// Connect to database. Because this is asynchronous action we can't be sure when connection is available but thanks to query buffer we don't have to worry about that.
models.connect({ mongoUrl: process.env.DB_URI })
app.use(routes)

app.listen(3000, () => console.log(`Server listening on port 3000!`))

@animir
Copy link
Owner

animir commented May 26, 2019

@tero I see now, thanks. 81 line is for mongoose support, so there is just one line should be fixed. Native driver can't buffer commands, as I know.

Would you like to make a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants