抛出新的MongooseError(“Mongoose.prototype.connect()不再接受回调”);

a6b3iqyw  于 2023-03-18  发布在  Go
关注(0)|答案(1)|浏览(900)

i got this error in my terminal when i run my index.js file as a 'npx nodemon .\index.js'
this is my 'index.js':

const connectToMongo = require('./database');
connectToMongo();

this is my dabase.js:

const mongoose = require('mongoose')
const mongoURI = 'mongodb://localhost:27017/'

const connectToMongo = ()=>{
    mongoose.connect(mongoURI,()=>{
        console.log('connected to mongo')
    })
}

module.exports = connectToMongo;

this is my terminal error:

throw new MongooseError('Mongoose.prototype.connect() no longer accepts a callback');
      ^

MongooseError: Mongoose.prototype.connect() no longer accepts a callback at Mongoose.connect (D:\React\inotebook\backend\node_modules\mongoose\lib\index.js:400:11) at connectToMongo (D:\React\inotebook\backend\database.js:5:14) at Object. (D:\React\inotebook\backend\index.js:2:1) at Module._compile (node:internal/modules/cjs/loader:1254:14) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Module._load (node:internal/modules/cjs/loader:958:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47 Node.js v18.14.1
i have not so much idea about nodemon so help me

f2uvfpb9

f2uvfpb91#

如错误所述,connect不再支持回调:

const mongoURI = 'mongodb://127.0.0.1:27017/'

const connectToMongo = ()=>{
    mongoose.connect(mongoURI)
        .then(() => console.log('connected to mongo'))
        .catch((err) => console.log('Connection failed'));
}

相关问题