Node.js 无法连接到 Mongo 数据库

Node.js can't connect to Mongo DB

我是 Node.js 的新手。因此,我尝试使用解析示例将我的 Node.js 应用程序与 Mongo 数据库连接起来。所以在我安装了所有东西之后,更改了 Master 和 AppID(在 Index.js 和 script.js 中),我尝试启动我的应用程序(npm start)然后出现这些警告。

>     > parse-server-example@1.4.0 start C:\Users\TAASCTI5\temp\parseex\parse-server-e
>     xample
>     > node index.js
>     
>     DATABASE_URI not specified, falling back to localhost.
>     parse-server-example running on port 1337.
>     info: Parse LiveQuery Server starts running
>     warn: Unable to ensure uniqueness for usernames:  MongoError: failed to connect
>     to server [localhost:27017] on first connect
>         at Pool.<anonymous> (C:\Users\TAASCTI5\temp\parseex\parse-server-example\nod
>     e_modules\parse-server\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> (C:\Users\TAASCTI5\temp\parseex\parse-server-examp
>     le\node_modules\parse-server\node_modules\mongodb-core\lib\connection\pool.js:26
>     0:12)
>         at Connection.g (events.js:291:16)
>         at emitTwo (events.js:106:13)
>         at Connection.emit (events.js:191:7)
>         at Socket.<anonymous> (C:\Users\TAASCTI5\temp\parseex\parse-server-example\n
>     ode_modules\parse-server\node_modules\mongodb-core\lib\connection\connection.js:
>     162:49)
>         at Socket.g (events.js:291:16)
>         at emitOne (events.js:96:13)
>     warn: Unable to ensure uniqueness for user email addresses:  MongoError: failed
>     to connect to server [localhost:27017] on first connect
>         at Pool.<anonymous> (C:\Users\TAASCTI5\temp\parseex\parse-server-example\nod
>     e_modules\parse-server\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> (C:\Users\TAASCTI5\temp\parseex\parse-server-examp
>     le\node_modules\parse-server\node_modules\mongodb-core\lib\connection\pool.js:26
>     0:12)
>         at Connection.g (events.js:291:16)
>         at emitTwo (events.js:106:13)
>         at Connection.emit (events.js:191:7)
>         at Socket.<anonymous> (C:\Users\TAASCTI5\temp\parseex\parse-server-example\n
>     ode_modules\parse-server\node_modules\mongodb-core\lib\connection\connection.js:
>     162:49)
>         at Socket.g (events.js:291:16)
>         at emitOne (events.js:96:13)
>     (node:18132) UnhandledPromiseRejectionWarning: Unhandled promise rejection (reje
>     ction id: 4): MongoError: failed to connect to server [localhost:27017] on first
>      connect
>     (node:18132) UnhandledPromiseRejectionWarning: Unhandled promise rejection (reje
>     ction id: 5): MongoError: failed to connect to server [localhost:27017] on first
>      connect

结果是我无法连接到我的 Mongo 数据库,出现错误 500 内部服务器错误。

当然,在您未提供的代码中发现问题是一种猜测,但我认为从错误消息中可以明显看出您没有 Mongo 数据库侦听 localhost:27017.

您没有提供数据库连接字符串 (DATABASE_URI),因此服务器正在尝试连接到本地主机。您在哪里托管您的 MongoDB 以及您在哪里托管您的服务器?

在您的 index.js 文件中,您需要包含数据库连接字符串:

var databaseUri = mongodb://...;

希望对您有所帮助。