我使用Node.Js,Expressjs,mLab(托管我的数据库)和mongoDB
我在Streetinform(我的数据库名称)上获得了未被授权执行命令的错误。 {listIndexes:" sessions",cursor:{}}
这是错误。
这就是我连接数据库的方式
var dbHost = process.env.DB_HOST || 'localhost';
var dbPort = process.env.DB_PORT || 3002;
var dbName = process.env.DB_NAME || 'Mongodb';
var dbURL = 'mongodb://'+dbHost+':'+dbPort+'/'+dbName;
if (app.get('env') == 'development'){
// prepend url with authentication credentials //
dbURL = 'mongodb://'+process.env.DB_USER+':'+process.env.DB_PASS+'@'+dbHost+':'+dbPort+'/'+dbName;
}
app.use(session({
secret: 'secret',
proxy: true,
resave: true,
saveUninitialized: true,
store: new MongoStore({ url: dbURL })
})
);
这是我的图书馆
var http = require('http');
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var errorHandler = require('errorhandler');
var cookieParser = require('cookie-parser');
var MongoStore = require('connect-mongo')(session);
var dotenv = require('dotenv')
dotenv.load();
var app = express();
建立与数据库的连接
var crypto = require('crypto');
var MongoDB = require('mongodb').Db;
var Server = require('mongodb').Server;
var moment = require('moment');
/*
ESTABLISH DATABASE CONNECTION
*/
var dbName = process.env.DB_NAME || 'mongodb';
var dbHost = process.env.DB_HOST || 'localhost'
var dbPort = process.env.DB_PORT || 3002;
var db = new MongoDB(dbName, new Server(dbHost, dbPort, {autoReconnect: true}), {w: 1});
db.open(function(err, d){
if (err) {
console.log(err);
} else {
if (process.env.NODE_ENV == 'live') {
db.authenticate(process.env.DB_USER, process.env.DB_PASS, function(e, res) {
if (e) {
console.log('mongo :: error: not authenticated', e);
}
else {
console.log('mongo :: authenticated and connected to database :: "'+dbName+'"');
}
});
} else{
console.log('mongo :: connected to database :: "'+dbName+'"');
}
}
});
答案 0 :(得分:0)
我使用mLabs,看来你没有创建数据库使用,因为它无法执行任何命令。
您可以使用以下步骤启用它:
现在,当您使用在步骤5中输入的用户名和密码验证传递时