1. 程式人生 > 其它 >nodejs連線MongoDB資料庫執行時報錯

nodejs連線MongoDB資料庫執行時報錯

技術標籤:node.js

解決:(node:13080) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version.

用node連結mongo出現警告資訊:
(node:13080) 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.

(Use node --trace-deprecation ... to show where the warning was created)
(node:13080) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
資料庫連線成功

解決:(節點:13080)不推薦使用警告:當前的URL字串分析器已不推薦使用,將在將來的版本中刪除。要使用新的解析器,請將選項{useNewUrlParser:true}傳遞給MongoClient.connect連線.
(節點:13080)棄用警告:當前伺服器發現和監視引擎已棄用,將在將來的版本中刪除。要使用新的伺服器發現和監視引擎,請將選項{useUnifiedTopology:true}傳遞給MongoClient建構函式。

// 引入mongoose模組
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/playground'
) .then(() => console.log('資料庫連線成功')) .catch(err => console.log('資料庫連線失敗', err))

報錯:
在這裡插入圖片描述
解決方法:將{ useNewUrlParser: true }和{ useUnifiedTopology: true }作為引數傳遞進去

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/playground', { 
useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => console.log('資料庫連線成功'))
    .catch(err => console.log('資料庫連線失敗', err))

剛開始學node.js,把遇到的錯誤記錄下來,以後忘記了再回來看