【发布时间】:2016-09-22 15:47:45
【问题描述】:
我正在尝试使用 node.js 在我的 Windows 系统上连接 mongodb。但是,它给我一个错误:
C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\lib\mongo_client.js:225
throw err
^
MongoError: failed to connect to server [127.0.0.1:27017] on first connect
at null.<anonymous> (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:313:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:260:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:162:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
这是我的代码:
"use strict";
const MongoClient = require('mongodb').MongoClient;
const dbUrl = 'mongodb://127.0.0.1:27017/cs602';
MongoClient.connect(dbUrl, (err, db) => {
if (err)
{throw err}else {
console.log('Successfully connected to',
db.s.databaseName);
}
// Do database operations
db.close();
});
我是 node.js 和 mongodb 的新手,有人可以帮助我吗?提前非常感谢您!
【问题讨论】:
-
在最常见的情况下,这意味着 mongodb 没有运行或者它是在另一个端口上启动的。你应该检查你的 mongo db 配置文件,并检查你是否已经启动了服务器
-
您可能还想查看 mongodb 日志以及解决 mongodb 问题的这些问题:stackoverflow.com/questions/16245805/… 和 stackoverflow.com/questions/16245805/…
-
@VasilDininski 非常感谢!我在你给我的那些链接中找到了解决方案!