我的应用程序在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
我似乎无法隔离随机导致身份验证错误的内容。
答案 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 */ }
);