mongoose連接數據庫(connect函數)


數據庫的連接

const mongoose = require('mongoose')
// 如果數據庫(database)不存在就會創建
mongoose.connect('mongodb://localhost/eg', 
  { useNewUrlParser: true, useUnifiedTopology: true },
  function (err) {
    if (err)
      console.log('連接數據庫失敗');
    else {
      console.log('連接數據庫成功');
    }
  })
  • mongoose.connect函數
function connect(
  uris: string, 
  options: mongoose.ConnectOptions, 
  callback: (err: MongoError) => void
)
//Opens the default mongoose connection. 
  1. 函數返回promise
  2. uris:'mongoose://username:password@ipadress:port/databaseName'
  3. options:其中的設置可以覆蓋uris中的設置


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM