skip to Main Content

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
create

serve 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


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. Add mongoose.set('strictQuery', false); where you user connecting the MongoDB server.

     mongoose.set('strictQuery', false);
     const conn = await mongoose.connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    });
    
    Login or Signup to reply.
  4. Mongoose supports a separate strictQuery option to avoid strict mode for query filters. This is because empty query filters cause Mongoose to return all documents in the model, which can cause issues.

    Provide mongoose.set(‘strictQuery’,true); to solve your issue

        const dotenv = require('dotenv');
    const mongoose = require('mongoose');
    const app = express();
    dotenv.config();
    
    mongoose.set('strictQuery', true);
    
    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}`);
    });
    
    Login or Signup to reply.
  5. Enter image description here

    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.

    Login or Signup to reply.
  6. You can try it…

    const dbSetup = async () => {
      try {
        const DBURL = process.env.DB_URL;
        mongoose.set("strictQuery", false);
        mongoose.connect(DBURL, {
          useNewUrlParser: true,
          ssl: true,
          sslValidate: false,
        });
      } catch (err) {
        console.log("Databse Connection Error : " + err.message);
      }
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search