我的 server.js 檔案中有這個 mongoose.connect 陳述句:
const db = mongoose.connect(config.db, {
'useNewUrlParser': true,
'useUnifiedTopology': true,
'useFindAndModify': false,
'useCreateIndex': true
}, function(err, database) {
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(chalk.red(err));
}else{
console.log("db connected");
}
});
// Init the express application
const app = require('./config/express')(db);
但是當我db在我的 express 檔案中參考常量時,它會一直顯示為undefined.
我在這里做錯了什么?
uj5u.com熱心網友回復:
你不應該向它傳遞任何回呼。在這種情況下,貓鼬會回傳一個 Promise,你需要await或.then(). 例如
mongoose.connect(config.db, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
}).then((db) => {
// Init the express application
const app = require('./config/express')(db);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/467803.html
