[英]Node Can't connect to mongodb using mongoose
So I'm on a macbook running mongodb locally. 所以我在本地运行mongodb的Macbook上。 Mongodb is listening on port 27017 and I can see it saying it's ready to accept connections.
Mongodb正在侦听端口27017,我可以看到它说已准备好接受连接。 If I open a mongo shell, I can see it shows the connection.
如果打开mongo shell,我可以看到它显示了连接。 When I run "node index.js", the program just hangs and doesn't show and error or it doesn't show connected.
当我运行“ node index.js”时,该程序只是挂起,没有显示和错误,或者没有显示已连接。 Also, in the mongo server tab I can see connections accepted
另外,在mongo服务器标签中,我可以看到接受的连接
Here's my code: 这是我的代码:
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var _ = require('lodash');
//create application
var app = express();
//add middleware
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(methodOverride('X-HTTP-Method-Overrise'));
mongoose.connect('mongodb://localhost:27017/boost', function(err) {
if (err) {
console.log(err);
}else{
console.log("Connected");
}
});
try this 尝试这个
mongoose.connect('mongodb://localhost:27017/boost', {useMongoClient:true});
mongoose.connection.once('open',function () {
console.log('Connected');
}).on('error',function (error) {
console.log('CONNECTION ERROR:',error);
});
There wasn't really anything wrong. 真的没有什么错。 I solved it by just doing and then writing my code later.
我先解决问题,然后再编写代码。
mongoose.connect('mongodb://localhost/boost');
var db = mongoose.connection;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.