I am just making a database called Fruits from my app.js
and connecting the database to MongoDB using Mongoose.
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/fruitsDB", {useNewUrlParser: true});
mongoose.set('strictQuery', false);
const fruitSchema = new mongoose.Schema({
name: String,
rating: Number,
review: String
});
const Fruit = mongoose.model("Fruit", fruitSchema);
const fruit = new Fruit({
name: "Apple",
rating: 7,
review: "Taste Good"
});
fruit.save();
Whenever I try node app.js
, I am getting DeprecationWarning. Even though I tried using mongoose.set('strictQuery', true);
, the same error continues as follows:
(node:15848) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option w
ill be switched back to `false` by default in Mongoose 7. Use `mongoose.set('str
ictQuery', false);` if you want to prepare for this change. Or use `mongoose.set
('strictQuery', true);` to suppress this warning.
(Use `node --trace-deprecation ...` to show where the warning was created)
D:\Web Development\FruitsProject\node_modules\mongoose\lib\drivers\node-mongodb-
native\collection.js:158
const err = new MongooseError(message);
^
MongooseError: Operation `fruits.insertOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (D:\Web Development\FruitsProject\node_modules\mongoo
se\lib\drivers\node-mongodb-native\collection.js:158:23)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7)
Node.js v18.12.1
And then the second error also continues fruits.insertOne().
Because of this, my MongoDB database is not getting updated.
test> show dbs
admin 40.00 KiB
config 108.00 KiB
local 40.00 KiB
shopDB 72.00 KiB
I just want to fix this error. But I don't know where to fix this error. For the second part of the error, it seems like it is coming from the node_modules itself. How can I fix this error?
mongoose.set("strictQuery", false);
mongoose.connect(process.env.MONGO_URL);
OR
mongoose.set("strictQuery", false);
mongoose.connect(process.env.MONGO_URL, () => {
console.log("Connected to MongoDB");
});
const connectDB = async () => {
try {
mongoose.set('strictQuery', false);
await mongoose.connect(db, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('MongoDB Connected...');
} catch (err) {
console.error(err.message);
// make the process fail
process.exit(1);
}