Home > Back-end >  how to fix Mongoose Deprecation Warning "the strictQuery"
how to fix Mongoose Deprecation Warning "the strictQuery"

Time:12-09

when i start build my backend server i get this deprecation warning, but its show that im connected to database. i just search the solution in youtube and re-create again but its still didnt work. this is my code ...

in 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}`);
});

in 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 enter image description here

its show :

(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 create

serve at http://localhost:5500

connected to db

i dont know where to fix this error because i think its come from my node_modules. how can i fix this warning? is this warning gonna impact when im gonna connect my frontend to backend or its gonna impact when im gonna deploy? sorry im just beginner.

i start build my backend server i get this deprecation warning, but its show that im connected to database. i just want fix the warning. i re-create this backend again but its still didnt work, its still show the deprecation warning

CodePudding user response:

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 to true globally to suppress the warning with:

const dotenv = require('dotenv');
const mongoose = require('mongoose');
const app = express();
dotenv.config();

mongoose.set('strictQuery', true);

Or, set the flag to false if you want to override the current strictQuery behavior and prepare for the new release:

const dotenv = require('dotenv');
const mongoose = require('mongoose');
const app = express();
dotenv.config();

mongoose.set('strictQuery', false);

Either way the warning should disappear.

For more information on why strictQuery will be brought back to false by default see here.
For more information on strictQuery see here.

CodePudding user response:

Go to Network Access on your mongodb page in browser. Then click button ADD IP ADRESS and add adress "0.0.0.0" It helps you to connect

  • Related