0

MongooseError: Mongoose.prototype.connect() no longer accepts a callback at Mongoose.connect

throw new MongooseError('Mongoose.prototype.connect() no longer accepts a callback'); ^ The dependencies in the project are: "dependencies": { "express": "^4.18.2", "mongoose": "^7.0.3" }, "devDependencies": { "nodemon": "^2.0.22" } Following is the code snippet in 'db.js': const mongoose = require("mongoose"); const mongoURI = "mongodb://localhost:27017/"; const connectToMongo = () => { mongoose.connect(mongoURI, () => { console.log("Connected to Mongo Successfully!"); }); }; module.exports = connectToMongo; Also, the code snippet from 'index.js': const connectToMongo = require("./db"); connectToMongo(); For further info, I'll attach the screenshots below: https://www.sololearn.com/post/1731676/?ref=app

16th Apr 2023, 6:14 PM
Asha
Asha - avatar
2 ответов
+ 1
This solution worked for me: https://stackoverflow.com/questions/75774347/throw-new-mongooseerrormongoose-prototype-connect-no-longer-accepts-a-callba These are the changes need to be made in 'db.js': const mongoose = require("mongoose"); const mongoURI = "mongodb://localhost:27017/"; const connectToMongo = async () => { try { mongoose.set("strictQuery", false); mongoose.connect(mongoURI); console.log("Connected to Mongo Successfully!"); } catch (error) { console.log(error); } }; module.exports = connectToMongo; ----------------------------------------- TERMINAL MESSAGE: [nodemon] restarting due to changes... [nodemon] starting `node .index.js index.js` Connected to Mongo Successfully!
16th Apr 2023, 6:35 PM
Asha
Asha - avatar
+ 1
The Error will be resolved after changing your code with this: const mongoose = require("mongoose"); const <your given name> = "mongodb://localhost:27017/"; const <your given name> = async () => { try { mongoose.set("strictQuery", false); mongoose.connect(<your given name>); console.log("Connected to MongoDB Successfully!"); } catch (error) { console.log(error); } }; module.exports = <your given name>;
2nd Jan 2024, 8:46 PM
Muhammad Zahak
Muhammad Zahak - avatar