简体   繁体   中英

Node.js can't connect to Mongo DB

I am new to Node.js. So I tried the parse-example to connect my Node.js app with a Mongo DB. So after I installed everything, changed the Master and the AppID (in the Index.js and script.js), I tried to start my app (npm start) and then these Warning appears.

>     > 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

The result is that i can't connect to my Mongo DB with an Error 500 internal Server Error.

当然,在您未提供的代码中查找问题是一种猜测,但是我认为从错误消息中可以明显看出,您没有在本地主机上监听Mongo的数据库:27017。

You didn't provide a database connection string (DATABASE_URI), so the server is trying to connect to localhost. Where are you hosting your MongoDB and where are you hosting your server?

In your index.js file, you need to include your database connection string:

var databaseUri = mongodb://...;

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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