Deprecated code in server.js in the back end development curriculum?

exercise 3 in MongoDB and Mongoose section

const removeOne = require("./myApp.js").removeById;
router.post("/remove-one-person", function (req, res, next) {
//change to Person.deleteOne()
  Person.remove({}, function (err) {
    if (err) {
      return next(err);
    }
    let t = setTimeout(() => {
      next({ message: "timeout" });
    }, TIMEOUT);
    let p = new Person(req.body);
    p.save(function (err, pers) {
      if (err) {
        return next(err);
      }
      try {
        removeOne(pers._id, function (err, data) {
          clearTimeout(t);
          if (err) {
            return next(err);
          }
          if (!data) {
            console.log("Missing `done()` argument");
            return next({ message: "Missing callback argument" });
          }
          console.log(data);
          Person.count(function (err, cnt) {
            if (err) {
              return next(err);
            }
            data = data.toObject();
            data.count = cnt;
            console.log(data);
            res.json(data);
          });
        });
      } catch (e) {
        console.log(e);
        return next(e);
      }
    });
  });
});

const removeMany = require("./myApp.js").removeManyPeople;
router.post("/remove-many-people", function (req, res, next) {
//Change to Person.deleteMany()
  Person.remove({}, function (err) {
    if (err) {
      return next(err);
    }
    let t = setTimeout(() => {
      next({ message: "timeout" });
    }, TIMEOUT);
    Person.create(req.body, function (err, pers) {
      if (err) {
        return next(err);
      }
      try {
        removeMany(function (err, data) {
          clearTimeout(t);
          if (err) {
            return next(err);
          }
          if (!data) {
            console.log("Missing `done()` argument");
            return next({ message: "Missing callback argument" });
          }
          Person.count(function (err, cnt) {
            if (err) {
              return next(err);
            }
            if (data.ok === undefined) {
              // for mongoose v4
              try {
                data = JSON.parse(data);
              } catch (e) {
                console.log(e);
                return next(e);
              }
            }
            res.json({
              n: data.n,
              count: cnt,
              ok: data.ok,
            });
          });
        });
      } catch (e) {
        console.log(e);
        return next(e);
      }
    });
  });
});

I believe Model.remove() needs to be changed to either Model.deleteOne() or Model.deleteMany(), with Model being Person in this case.

Did you follow these instructions from the first lesson:

Add mongodb@~3.6.0 and mongoose@~5.4.0 to the project’s package.json . Then, require mongoose as mongoose in myApp.js . Create a .env file and add a MONGO_URI variable to it. Its value should be your MongoDB Atlas database URI. Be sure to surround the URI with single or double quotes, and remember that you can’t use spaces around the = in environment variables. For example, MONGO_URI='VALUE' . When you are done, connect to the database using the following syntax:

You need to be using the same versions as the challenge

hmm, I had thought so. i used “npm install mongodb@~3.6.0”.
resulting in:

"dependencies": {
        "body-parser": "^1.15.2",
        "dotenv": "^8.2.0",
        "express": "^4.12.4",
        "mongodb": "^3.6.0",
        "mongoose": "^5.4.0"
    }

if i change it to @~ from the ^ it does not work for me. “Replit: Package operation failed.”
sorry if i am not understanding the @ meta character, still learning lol. Do not mean to irritate.

Ok, so the @ was confusing me. I see now that if i change it to the tilde instead of ^ to use the latest patch version instead of minor lol.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.