Mongodb + nodejs doesn't connect with localhost

时间:2021-03-23 03:05:47

I'm keep trying myself to connect local mongo server via nodejs. Here's simple tutorial given by official mongodb, but doesn't hook once part.

我一直在尝试通过nodejs连接本地mongo服务器。这是官方mongodb给出的简单教程,但不会挂钩一次。

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
    // we're connected!
    console.log("connected");
});

here's package.json of my project:

这是我项目的package.json:

{
    "name": "MVC-Express-Application",
    "description": "An Express.js application using the MVC design pattern...",
    "version": "0.0.1",
    "dependencies": {
        "body-parser": "1.4.3",
        "errorhandler": "1.1.1",
        "express": "4.4.4",
        "express-handlebars": "1.1.0",
        "mongoose": "^3.8.8",
        "morgan": "1.1.1"
    }
}

when i type mongo they say

当我输入mongo他们说

MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
Server has startup warnings:
2017-11-07T07:58:20.003+0900 I CONTROL  [initandlisten]
2017-11-07T07:58:20.003+0900 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-11-07T07:58:20.003+0900 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-11-07T07:58:20.003+0900 I CONTROL  [initandlisten]

so it should say connected on console but doesn't. Anybody knows how to deal with it? I tried reinstalling mongo via brew.

所以它应该说在控制台上连接但不是。有谁知道如何处理它?我尝试通过brew重新安装mongo。

Thanks.

谢谢。

macOS Sierra 10.12.6 node v7.6.0

macOS Sierra 10.12.6 node v7.6.0

2 个解决方案

#1


0  

I think you have to start the mongodb process, and for that run mongod in the Server/3.4/bin/ directory. Try connecting after that.

我认为你必须启动mongodb进程,并在Server / 3.4 / bin /目录中运行mongod。之后尝试连接。

#2


0  

Updating to the newest mongoose solved the problem right away! Thanks guys for your help!

更新到最新的mongoose立即解决了问题!谢谢大家帮助!

#1


0  

I think you have to start the mongodb process, and for that run mongod in the Server/3.4/bin/ directory. Try connecting after that.

我认为你必须启动mongodb进程,并在Server / 3.4 / bin /目录中运行mongod。之后尝试连接。

#2


0  

Updating to the newest mongoose solved the problem right away! Thanks guys for your help!

更新到最新的mongoose立即解决了问题!谢谢大家帮助!