-
-
Notifications
You must be signed in to change notification settings - Fork 163
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
Comments
@tero Hi, could you paste rate limiter config here and which nodejs library you use for MongoDB? |
For example this will fail. 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. |
@tero Is it correct, that you use |
@tero Which version of mongoose do you use? |
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!`)) |
@tero I see now, thanks. 81 line is for Would you like to make a PR? |
I'm implementing rate limit functionality with RateLimiterMongo and
_initCollection
function throwsTypeError: Cannot read property 'collection' of undefined
. Changingthis.client.db
tothis.client
from rows 80 and 81 will fix the issue.This is with MongoDB version 3.4.10.
The text was updated successfully, but these errors were encountered: