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

问题描述 投票:0回答:1

我的应用程序在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

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

node.js mongodb mongoose mlab
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.