mongodb TypeError:连接.once(...).catch不是函数

w6lpcovy  于 2022-11-03  发布在  Go
关注(0)|答案(1)|浏览(126)
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)
zdwk9cvp

zdwk9cvp1#

.once之后的.on
https://www.tabnine.com/code/javascript/functions/mongoose/Connection/once

mongoose.connection
    .once('open', function () {
      console.log('MongoDB running');
    })
    .on('error', function (err) {
      console.log(err);
    });

相关问题