require('dotenv').config();
const mongoose = require('mongoose');
function connectDB() {
// Database connection 🥳
mongoose.connect(process.env.MONGO_CONNECTION_URL,
{ useNewUrlParser: true, useCreateIndex:true, useUnifiedTopology: true, useFindAndModify : true });
const connection = mongoose.connection;
connection.once('open', () => {
console.log('Database connected ');
}).catch(err => {
console.log('Connection failed ');
});`
}每次我都得到catch不是connectDB错误中的函数的错误:
C:\Users\user\Desktop\mern\config\db.js:15
}).catch(err => {
^
TypeError: connection.once(...).catch is not a function
at connectDB (C:\Users\user\Desktop\mern\config\db.js:15:13)
1条答案
按热度按时间zdwk9cvp1#
在
.once
之后的.on
https://www.tabnine.com/code/javascript/functions/mongoose/Connection/once