我正在运行这个节点应用程序,当我用我的mongodb在本地运行时,它运行正常,但是在尝试连接到Mlab时,但我不断得到一些奇怪的错误,我无法弄明白错误是
错误讯息:
[nodemon] starting `node index.js`
Error connecting to mongodb!
[nodemon] clean exit - waiting for changes before restart
index.js
const con = require('./con');
const mongoose = require('mongoose');
mongoose.Promise = con.mongoose.Promise;
mongoose.connect(con.mongoose.connection)
.then(() => {
console.log('Connected to mongodb!');
require('./app.js');
})
.catch(err => {
console.log('Error connecting to mongodb!');;
});
con.js :
module.exports = {
port: process.env.NODE_PORT || 8080,
mongoose: {
Promise: require('bluebird'), //mongoose promise library
//connection: 'mongodb://localhost:27017/mmmmm',
mongoose.createConnection(''mongodb://[username:password@]host1');
}
};
这是我的 app.js :
const express = require("express"),
compress = require("compression"),
bodyParser = require("body-parser"),
cookieParser = require("cookie-parser"),
mongoose = require("mongoose");
session = require("express-session"),
bcrypt = require("bcrypt"),
logger = require("morgan"),
config = require('./con');
const app = express();
app.enabled('trust proxy');
app.use(logger('dev'));
app.use(compress());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(function(req, res, next){
if (req.session && req.session.userId)
req.isLoggedIn = true;
else
req.isLoggedIn = false;
next();
});
app.use('/', require('./routes/router'));
app.listen(config.port, () => {
utils.info('Express server listening on port ' + con.port);
utils.info('http://localhost:' + con.port);
});
我也尝试过:
module.exports = {
port: process.env.NODE_PORT || 8080,
mongoose: {
Promise: require('bluebird'), //mongoose promise library
},
url : 'mongodb://[username:password@]host1'
};
请有人帮助我,我已经爬了SO,但没有解决方案。
由于
答案 0 :(得分:0)
如果您使用mongoose 5.x,请删除
{ useMongoClient : true }
因为在mongoose 5.x中不再需要该选项