Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: `useFindAndModify` is an invalid option [closed]

I am new to MERN stack and I am following MERN stack tutorial on YouTube. I got an error on Mongoose.

Error: `useFindAndModify` is an invalid option

I couldn't find any solution to that.

    import express from "express";
    import bodyParser from "body-parser";
    import mongoose from "mongoose";
    import cors from "cors";


    const app = express();
    app.use(bedyParser.json({ limit: "30mb", extended: true }));
    app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));

    app.use(cors());

    const CONNECTION_URL =
    "mongodb+srv://myratcharyyev:<password>@clustero.mn9xi.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
    const PORT = process.env.PORT || 5000;
    mongoose
    .connect(CONNECTION_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true
    // useCreateIndex: true
    })
    .then(( =>
    app.listen (PORT, () => console.log("Server running on port: ${PORT}'))
    )
    .catch((error) => console.log(error message));
    mongoose.set("useFindAndModify", false);
like image 762
Myrat Avatar asked Sep 02 '21 13:09

Myrat


People also ask

Is usefindandmodify not supported on mongoose?

But now 'the options [useFindAndModify] is not supported' is coming on running the app. Show activity on this post. For others who are facing this issue, useFindAnyModify is not supported if you are using mongoose version 6+. To solve this either we can remove useFindAnyModify or downgrade mongoose version to use v5+.

Is findoneandupdate () and findandmodify () deprecated?

DeprecationWarning: Mongoose: findOneAndUpdate () and findOneAndDelete () without the useFindAndModify option set to false are deprecated. DeprecationWarning: collection.findAndModify is deprecated.

Is the collection findandmodify deprecated?

DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead. But now 'the options [useFindAndModify] is not supported' is coming on running the app.


2 Answers

It's a deprecated now. // No longer necessary:

mongoose.set('useFindAndModify', false);

await mongoose.connect('mongodb://localhost:27017/test', {
  useNewUrlParser: true, // <-- no longer necessary
  useUnifiedTopology: true // <-- no longer necessary
});

use this line of code

mongoose.connect(CONNECTION_URL).then(()=>{console.log('...')})

https://mongoosejs.com/docs/migrating_to_6.html#mongoose-connect-returns-a-promise

like image 118
Bahram Bayramli Avatar answered Nov 08 '22 19:11

Bahram Bayramli


You are explicitly setting useFindAndModify with .set(). Remove the line below:

mongoose.set("useFindAndModify", false);
like image 37
NeNaD Avatar answered Nov 08 '22 21:11

NeNaD