【问题标题】:Mean.js not working. Could not connect to MongoDBMean.js 不工作。无法连接到 MongoDB
【发布时间】:2017-06-17 10:45:32
【问题描述】:

情况:

我下载了这个存储库:https://github.com/meanjs/mean

遵循并执行所有指令。

$ npm start,得到以下错误:


错误:

Could not connect to MongoDB! { MongoError: failed to connect to server [localhost:27017] on first connect


P.S.:我确实安装了 Mongodb 并检查它是否与 $ mongod 一起使用。


编辑:

如果我这样做$ npm start,则会显示完整的错误消息:

+ Important warning: config.domain is empty. It should be set to the fully qualified domain of the app.
Could not connect to MongoDB!
{ MongoError: failed to connect to server [localhost:27017] on first connect
    at Pool.<anonymous> (/Users/TLL/Desktop/Web Learn/JobBoard/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:313:35)
    at emitOne (events.js:96:13)
    at Pool.emit (events.js:188:7)
    at Connection.<anonymous> (/Users/TLL/Desktop/Web Learn/JobBoard/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:271:12)
    at Connection.g (events.js:291:16)
    at emitTwo (events.js:106:13)
    at Connection.emit (events.js:191:7)
    at Socket.<anonymous> (/Users/TLL/Desktop/Web Learn/JobBoard/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:165:49)
    at Socket.g (events.js:291:16)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at emitErrorNT (net.js:1278:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
  name: 'MongoError',
  message: 'failed to connect to server [localhost:27017] on first connect' }
(node:5283) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: failed to connect to server [localhost:27017] on first connect
/usr/local/bin/node[5283]: ../src/debug-agent.cc:149:void node::debugger::Agent::Stop(): Assertion `(err) == (0)' failed.
 1: node::Abort() [/usr/local/bin/node]
 2: node::RunMicrotasks(v8::FunctionCallbackInfo<v8::Value> const&) [/usr/local/bin/node]
 3: node::debugger::Agent::~Agent() [/usr/local/bin/node]
 4: node::debugger::Agent::~Agent() [/usr/local/bin/node]
 5: node::Environment::~Environment() [/usr/local/bin/node]
 6: node::Start(int, char**) [/usr/local/bin/node]
 7: start [/usr/local/bin/node]
[21:34:44] [nodemon] app crashed - waiting for file changes before starting...

如果我这样做会出错$ mongo:

MongoDB shell version v3.4.1
connecting to: mongodb://127.0.0.1:27017
2017-01-31T21:41:14.839+0100 W NETWORK  [main] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-01-31T21:41:14.840+0100 E QUERY    [main] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:234:13
@(connect):1:6
exception: connect failed

【问题讨论】:

  • 尝试127.0.0.1而不是本地主机
  • 如果你从命令行运行mongo,它会起作用吗?
  • @Aᴍɪʀ 不,我刚试过。 $mongod 有效,但 $mongo 无效,让我用mongo 的错误代码编辑问题
  • @Coder1000 mongod 是 mongodb 的实际实例。让它运行,然后在另一个终端中运行你的应用程序。
  • @Aᴍɪʀ 如果我做 $mongod,那么我不能做 $npm start,原谅我的无知:我该怎么做?

标签: javascript node.js mongodb npm mean-stack


【解决方案1】:

在命令行中使用mongod启动MongoDB实例并让它运行。

然后在另一个终端上运行npm start。应该先运行 MongoDB 实例。

要查看您可以传递给mongod 的完整选项,您可以查看documentations

根据您的操作系统,您可以搜索如何让 MongoDB 实例在启动时运行,这样您就不必在单独的窗口中手动运行它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-02
    • 2014-01-13
    • 2012-02-01
    • 2016-07-31
    • 2014-11-04
    • 2013-07-27
    相关资源
    最近更新 更多