UnhandledPromiseRejectionWarning:MongoNetworkError:首次连接时无法连接到服务器[localhost:27017] [MongoNetworkError

时间:2018-04-24 13:57:22

标签: javascript node.js mongodb express handlebars.js

我尝试运行以下命令:

    success connection to port 3000
(node:16767) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError
: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (/Users/hatchery/Documents/nodejs/fxexpress/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:503:11
)
    at emitOne (events.js:116:13)
    at Pool.emit (events.js:211:7)
    at Connection.<anonymous> (/Users/hatchery/Documents/nodejs/fxexpress/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:326:12)
    at Object.onceWrapper (events.js:317:30)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at Socket.<anonymous> (/Users/hatchery/Documents/nodejs/fxexpress/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:245:50)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:64:8)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
(node:16767) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:16767) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

但是,我从终端获得以下信息:

java.lang.IllegalArgumentException: Could not resolve placeholder 'custom.resource' in value "#{${custom.resource}}"

我不确定为什么会收到此错误。有人可以建议做什么吗?

10 个答案:

答案 0 :(得分:6)

不确定您使用的系统,但如果您使用的是Linux,请尝试运行此系统:

mongo

或对于macOS:

mongod

让它在新终端中运行,然后再次尝试运行服务器

答案 1 :(得分:6)

虽然上述解决方案可能有效,但最好还是了解如何调试以及错误说明的内容。

您得到UnhandledPromiseRejectionWarning,因为在您的代码中,您不得处理与MongoDB连接的promise promise情况。

接下来,节点服务器无法连接到mongodb。这可能是由于几个原因。 @Sayegh提到的一个简单案例,服务器本身没有运行。可能是其他原因,如错误的端口等,服务器不接受传入连接等。

如何调试。第一步是确定进程是否正在运行。一种简单的方法是运行此命令ps aux | grep mongo,它将所有用户的所有进程列为mongo作为文本。 如果这是真的仍然无法连接,请寻找端口。如果仍然无法连接,请检查服务器是否接受传入连接并从那里获取。

答案 2 :(得分:3)

如果您使用的是mongoDB.Atlas进入网络访问并将白名单条目设置为0.0.0.0/0。 enter image description here

enter image description here

重新运行命令。

答案 3 :(得分:0)

在启动NodeJS命令之前,请确保创建具有正确权限的正确目录:

> sudo mkdir -p /data/db
> sudo chmod -R go+w /data/db

答案 4 :(得分:0)

首先,我尝试了Skitty的解决方案,但该解决方案对我不起作用,但是我仍然将该解决方案保留在我的代码中,然后我更改了app.listen(process.env.PORT || 3000);,它对我有用。

答案 5 :(得分:0)

我遇到了同样的问题,但是经过了很多RND。我发现问题出在哪里,所以请在您的终端上运行此命令。

sudo服务mongod启动

然后在终端上运行 mongo 并查看。

答案 6 :(得分:0)

我遇到了同样的问题,并通过将数据库用户的用户名和密码更改为更简单的方式(例如 dev:dev )解决了问题,并且在使用 user-dev:user时遇到错误-dev ...只需将其更改为简单的

答案 7 :(得分:0)

只需修复系统中安装的mongodb。通过单击已安装的软件包,您可以选择修复它。

答案 8 :(得分:0)

如果您使用的是Mongo数据库图集,请继续进行网络访问并将其设置为当前IP地址,然后重新运行命令。

我希望它对您有用。

答案 9 :(得分:0)

enter image description here

MongoDB连接希望您输入URL编码的密码。但是当我用'。'作为密码时。 (xxx.xxx),该网址的网址编码仍为点。但是,它仍然无法正常工作。 我将密码更改为简单的所有小写字符串,并且效果很好。

不确定为什么不接受。但这解决了时间问题。

相关问题