简体   繁体   English

无法将Mongodb连接到MEAN堆栈的Node.js

[英]Can't connect Mongodb to Node.js, MEAN-stack

I'm a Mac user and I'm very new to MEAN-stack. 我是Mac用户,对MEAN-stack非常陌生。 I'm trying to connect a Mongoose with my Node.js but it always show this error 我正在尝试将Mongoose与我的Node.js连接,但它总是显示此错误

events.js:141
  throw er; // Unhandled 'error' event
  ^ 
Error: failed to connect to [undefined:27017]
at null.<anonymous> (/Users/toeysk/Desktop/testwebrtc.git/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:556:25)
at emitThree (events.js:97:13)
at emit (events.js:175:7)
at null.<anonymous> (/Users/toeysk/Desktop/testwebrtc.git/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:156:15)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (/Users/toeysk/Desktop/testwebrtc.git/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at connectErrorNT (net.js:996:8)
at doNTCallback2 (node.js:452:9)
at process._tickCallback (node.js:366:17)

I can use both Mongoose and Mongodb normally in my terminal. 我可以在终端上正常使用Mongoose和Mongodb。

Here is my server.js 这是我的server.js

process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var mongoose = require('./config/mongoose'); //connect DB
var express = require('./config/express');

var db = mongoose();
var app = express();
app.listen(3000);

module.exports = function(){
    require('../app/routes/index.routes')(app);
    require('../app/routes/user.routes')(app);
};

module.exports = app;

And it calls mongoose.js 它调用mongoose.js

var config = require('./config');
var mongoose = require('mongoose');

console.log('Mongoose.js');
console.log(config);

module.exports = function(){

    mongoose.set('debug', config.debug);

    var db = mongoose.connect(config.monoUri);

    console.log('Call User Model');
    require('../app/models/user.model');

    return db;
}; 

Then calls config.js and development.js 然后调用config.jsdevelopment.js

/////config.js/////

module.exports = require('./env/' + process.env.NODE_ENV + '.js');

/////development.js/////

module.exports = {
    debug: true,
    mongoUri: 'mongodb://localhost/webrtc',
    sessionSecret: 'dev_secret_key'
};

Please help. 请帮忙。 I have been trying to debug this about 2 days and I got nothing new. 我一直在尝试调试大约2天,但没有任何新内容。 Anything miss? 有什么想念的吗?

The error is pointing you towards the issue: 该错误将您引向该问题:

Error: failed to connect to [undefined:27017]

So there's something wrong with the location of the database, it looks like the hostname is undefined. 因此,数据库的位置出了点问题,看来主机名未定义。

Here's your connection code: 这是您的连接代码:

var db = mongoose.connect(config.monoUri);

And here your configuration: 这是您的配置:

mongoUri: 'mongodb://localhost/webrtc'

Can you spot the typo in your connection code? 您可以在连接代码中发现错字吗? It says monoUri , not mongoUri . 它说的是monoUri ,而不是mongoUri

can you do 你可以做

npm install mongoose

and also post the output from 并发布输出

ps -ef | grep mongod

All on the the machine you are running node from. 所有运行节点的计算机上的节点。 That would go a long way towards the rest of an answer. 这将对其余答案大有帮助。

Also follow the simple sample code- https://github.com/mongodb/node-mongodb-native#introduction , substitute the database name and a collection name and run that on node 还要遵循简单的示例代码-https ://github.com/mongodb/node-mongodb-native#introduction ,替换数据库名称和集合名称,然后在节点上运行

node sample.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM