2016-11-21 107 views
0

我是Node.js的新手。所以我嘗試了parse-example將我的Node.js應用程序與Mongo DB連接起來。所以在我安裝了所有東西之後,改變了Master和AppID(在Index.js和script.js中),我嘗試啓動我的應用程序(npm start),然後出現這些警告。Node.js無法連接到Mongo DB

>  > [email protected] 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 DB,出現錯誤500內部服務器錯誤。

+0

你需要發佈你的代碼......但是看起來你沒有在你的計算機上本地運行mongodb。它可能已安裝,但可能未運行。 – Randy

回答

0

您沒有提供數據庫連接字符串(DATABASE_URI),因此服務器正嘗試連接到本地主機。你在哪裏託管你的MongoDB,你在哪裏託管你的服務器?

在你index.js文件,你需要包括你的數據庫連接字符串:

var databaseUri = mongodb://...; 

希望有所幫助。

+0

它的作品知道,謝謝你,我的朋友。 –

0

當然,在您沒有提供的代碼中發現問題是一種猜測工作,但我認爲從錯誤消息中可以明顯看出,您沒有在localhost上監聽Mongo數據庫:27017。