數據庫的連接
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.
- 函數返回promise
- uris:'mongoose://username:password@ipadress:port/databaseName'
- options:其中的設置可以覆蓋uris中的設置