Loopback MongoError: not authorized for query on heroku_xxxx.role
Asked Answered
C

1

7

I'm using Loopback for my server with the following setup:

"loopback": "^2.36.0",
"loopback-boot": "^2.23.0",
"loopback-component-explorer": "^2.4.0",
"loopback-component-storage": "^1.9.1",
"loopback-connector-mongodb": "^1.17.0",
"loopback-datasource-juggler": "^2.53.0",

It's connecting to a mongoDB that's on mlab.com

My main query includes a model and it's dependencies, ex:

GET /api/course/1234
PARAMS filter = {
     include: [
          {relation:'students'},
          {relation:'documents'},
          {relation:'modules'}
     ]
}

99.9% of the time, it works perfectly. But when I start having more activity on the platform, I start getting this error before the server starts crashing. Here's the error:

MongoError: not authorized for query on db.student
at Function.MongoError.create (/app/node_modules/mongodb-core/lib/error.js:31:11)
at queryCallback (/app/node_modules/mongodb-core/lib/cursor.js:182:34)
at /app/node_modules/continuation-local-storage/context.js:76:17
at bound (domain.js:287:14)
at runBound (domain.js:300:12)
at Callbacks.emit (/app/node_modules/mongodb-core/lib/topologies/server.js:119:3)
at null.messageHandler (/app/node_modules/mongodb-core/lib/topologies/server.js:397:23)
at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:302:22)

Sometimes it's on db.students, sometimes it's on db.documents ... seems random and I have no idea how to fix it.

To connect to the DB, I use the URI provided by mlab. Which looks like this:

mongodb://<dbuser>:<dbpassword>@<dbpath>/<dbname>

With and without ?&authMode=scram-sha1&rm.tcpNoDelay=true, but still getting errors with.

Any ideas what's happening and how to fix this?

__ Edit: I've tried a couple of different versions with no luck. Latest is:

"loopback": "^2.22.0",
"loopback-boot": "^2.6.5",
"loopback-component-explorer": "^2.4.0",
"loopback-component-storage": "^1.9.1",
"loopback-connector-mongodb": "^1.17.0",
"loopback-datasource-juggler": "^2.39.0",

__ Edit: I found this post on Github: https://github.com/strongloop/loopback-connector-mongodb/issues/251 which seems to be the same error, I followed what the others said solved their problems by changing the [email protected] but still no luck...

Crypto answered 2/11, 2016 at 0:32 Comment(3)
you said that this error comes out when you have 'more activity' on the platform. All mlab mongodbs have the 'auth' flag, so you must provide your auth informations (and you already did it as i see). Maybe you have more dbs with different auth credentials but reachable from the same URI? or maybe different DBs for dev/staging/production environments? try to setup again your auth info, here's the docs: docs.mlab.com/connecting/#authenticationSulla
@AlbertoFecchi Well officialy, I seem to have a replica set : mongodb://<dbuser>:<dbpassword>@<db1>,<db2>/<dbname>?replicaSet=<number> . Does that help in any way?Crypto
@AlbertoFecchi It doesn't seem to be the activity, but happens after some idle time on the server. So when I reboot, everything works fine, then after ~5min of inactivity, the next request fails. And it always changes collection (from students, to documents, to roles, then documents,...).Crypto
B
4

The solution was to update Loopback to the latest version. The Loopback library includes the MongoDB Node driver as a dependency and uses the driver to connect to a MongoDB database.

Previous versions of the MongoDB Node driver have known connection/authentication issues. By updating to the latest Loopback version, a more recent version of the Node driver was installed.

Bipod answered 3/11, 2016 at 16:38 Comment(1)
Updated the main post. I just tried with a couple of different versions but none worked.Crypto

© 2022 - 2024 — McMap. All rights reserved.