When I start build my backend server, I get this deprecation warning, but it’s showing that I’m connected to the database. I just searched for a solution in YouTube and recreated it again, but it’s still showing the deprecation warning. This is my code:
File server.js
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const app = express();
dotenv.config();
mongoose
.connect(process.env.MONGODB_URI)
.then(() => {
console.log('connected to db');
})
.catch((err) => {
console.log(err.message);
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`serve at http://localhost:${port}`);
});
File package.json
{
"name": "backend",
"version": "1.0.0",
"description": "backend",
"main": "server.js",
"scripts": {
"start": "node server",
"dev": "nodemon server"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mongoose": "^6.8.0"
},
"devDependencies": {
"nodemon": "^2.0.20"
}
}
And this is the Mongoose deprecation warning:
Screenshot of Mongoose Deprecation Warning
It shows:
(node:8392) [MONGOOSE] DeprecationWarning: Mongoose: the
`strictQuery` o` if you want to prepare for this change. Or use
`mongoose.set(‘strictQu(Use `node –trace-deprecation …` to show where the warning was
createserve at http://localhost:5500
connected to db
I don’t know where to fix this error, because I think it comes from my node_modules folder folder.
How can I fix this warning? Is this warning going to be a problem when I connect my frontend to backend or is it going to be a problem when I deploy?
6
Answers
This warning was introduced to notify users about the change that will be introduced in Mongoose 7 to the default value of
strictQuery
.It’s default value will be brought back to
false
.You can either set the
strictQuery
option totrue
globally to suppress the warning with:Or, set the flag to
false
if you want to override the currentstrictQuery
behavior and prepare for the new release:Either way the warning should disappear.
For more information on why
strictQuery
will be brought back tofalse
by default see here.For more information on
strictQuery
see here.Go to Network Access on your MongoDB page in the browser. Then click button Add IP address and add address "0.0.0.0".
It helps you to connect.
Add
mongoose.set('strictQuery', false);
where you user connecting the MongoDB server.Provide mongoose.set(‘strictQuery’,true); to solve your issue
I also experience a similar kind of deprecation issue. And I added
mongoose.set('strictQuery', true)
as shown in the attached image and then solved the issue.You can try it…