简体   繁体   中英

Random error with Mongoose and Mongodb: Not authorized to execute command

My app is running NodeJS 9.5.0 and mongoose 5.1.3 on a MongoDB at Mlab. On occasion, I will get a not authorized to execute command and a simple restart of the node process fixes the issue. Any ideas what could be causing this?

My connection string:

// import environmental variables from our variables.env file
require('dotenv').config({ path: 'variables.env' });

// Connect to our Database and handle any bad connections
mongoose.connect(process.env.DATABASE);

And in my variables.env (user/pass/db numbers redacted):

DATABASE=mongodb://<DBUSER>:<DBPASS>@ds0000-a0.mlab.com:0000,ds000000-a1.mlab.com:00000/app?replicaSet=rs-ds00000

I can't seem to isolate what is randomly causing the authentication error.

Just random, huh. Perhaps add a callback or a promise and trap the error:

mongoose.connect(uri, options, function(error) {
  // Check error in initial connection. There is no 2nd param to the callback.
});

// Or using promises
mongoose.connect(uri, options).then(
  () => { /** ready to use. The `mongoose.connect()` promise resolves to 
  undefined. */ },
  err => { /** handle initial connection error */ }
);

Source: http://mongoosejs.com/docs/connections.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM