Mongoose和Mongodb的随机错误:未授权执行命令

时间:2018-06-11 16:26:09

标签: node.js mongodb mongoose mlab

我的应用程序在Mlab的MongoDB上运行NodeJS 9.5.0和mongoose 5.1.3。有时,我会得到一个not authorized to execute command,并且只需重新启动节点进程就可以解决问题。可能导致这种情况的任何想法?

我的连接字符串:

// 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);

在我的variables.env中(用户/传递/数据库编号已编辑):

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

我似乎无法隔离随机导致身份验证错误的内容。

1 个答案:

答案 0 :(得分:0)

随便,呵呵。也许添加回调或承诺并捕获错误:

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 */ }
);

来源:http://mongoosejs.com/docs/connections.html

相关问题