簡體   English   中英

使用mongoDB客戶端連接mongoDB時遇到問題

[英]facing problem in connecting mongoDB using the mongoDB client

我正在使用我的應用程序中的mongodb客戶端連接mongodb。

“ app.js”文件。

var MongoClient = require('mongodb').MongoClient

MongoClient.connect('mongodb://localhost:27017/animals', function (err, db) {
  if (err) throw err

//   db.collection('mammals').find().toArray(function (err, result) {
//     if (err) throw err

//     console.log(result)
//   })
})

問題是:

(node:16348) DeprecationWarning: current URL string parser is 
deprecated, and will be removed in a future version. To use the new 
parser, pass option { useNewUrlParser: true } to MongoClient.connect.
/home/amarjeet/Desktop/node2/node_modules/mongodb/lib/operations/mongo_client_ops.js:474
      throw err;
       ^

MongoNetworkError: failed to connect to server [localhost:27017] on 
first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (/home/amarjeet/Desktop/node2/node_modules/mongodb- 
core/lib/topologies/server.js:564:11)
    at Pool.emit (events.js:188:13)
    at Connection.<anonymous> 
(/home/amarjeet/Desktop/node2/node_modules/mongodb-core/lib/connection/pool.js:317:12)
    at Object.onceWrapper (events.js:276:13)
    at Connection.emit (events.js:188:13)
    at Socket.<anonymous> (/home/ amarjeet/Desktop/node2/node_modules/mongodb-core/lib/connection/connection.js:246:50)
    at Object.onceWrapper (events.js:276:13)
    at Socket.emit (events.js:188:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
[nodemon] app crashed - waiting for file changes before starting...

所以這是終端上顯示的問題,我不知道如何解決!

只需在連接中添加{useNewUrlParser:true}

var MongoClient = require('mongodb').MongoClient

MongoClient.connect('mongodb://localhost:27017/animals', {useNewUrlParser: true }, function (err, db) {
  if (err) throw err
//   db.collection('mammals').find().toArray(function (err, result) {
//     if (err) throw err

//     console.log(result)
//   })
})

您必須先在系統中安裝MongoDB數據庫服務器並啟動它。
如果已經安裝:
檢查服務器是否處於啟動狀態。 並嘗試與mongo shell連接
如果服務器也處於啟動狀態:
而不是像@Vaghani Janak所提到的那樣放{useNewUrlParser: true }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM