javascript Mongoose 错误:findById()不再接受回调

cbjzeqam  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(155)

Hi 👋 i've encountered a problem can anyone help, Thanking you in advance !
MONGOOSE VERSION: "mongoose": "^7.0.3"
MongooseError: Model.findById() no longer accepts a callback at Function.findById (C:\Users\NOOB\Desktop\mern-project\node_mod at module.exports.userInfo (C:\Users\NOOB\Desktop\mern-project\co at Layer.handle [as handle_request] (C:\Users\NOOB\Desktop\mern-project\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\NOOB\Desktop\mern-project\node_modules\express\lib\router\route.js:144:13) at Route.dispatch (C:\Users\NOOB\Desktop\mern-project\node_modules\express\lib\router\route.js:114:3) at Layer.handle [as handle_request] (C:\Users\NOOB\Desktop\mern-project\node_modules\express\lib\router\layer.js:95:5) at C:\Users\NOOB\Desktop\mern-project\node_modules\express\lib\router\index.js:284:15 at param (C:\Users\NOOB\Desktop\mern-project\node_modules\express\lib\router\index.js:365:14) at param (C:\Users\NOOB\Desktop\mern-project\node_modules\expressmern-project\node_modules\express\lib\routejs:376:1r\index.js:376:14) ï\Desktop\mern-project\no at Function.process_params (C:\Users\NOOB\Desktop\mern-project\node_modules\lib\rou\express\lib\router\index.js:421:3)

const UserModel = require("../models/user.model");
const ObjectID = require("mongoose").Types.ObjectId;

module.exports.getAllUsers = async (req, res) => {
  const users = await UserModel.find().select("-password");
  res.status(200).json(users);
};

module.exports.userInfo = (req, res) => {
  if (!ObjectID.isValid(req.params.id))
    return res.status(400).send("ID unknown : " + req.params.id);

  UserModel.findById(req.params.id, (err, docs) => {
    if (!err) res.send(docs);
    else console.log("ID unknown : " + err);
  }).select("-password");
};

module.exports.updateUser = async (req, res) => {
  if (!ObjectID.isValid(req.params.id))
    return res.status(400).send("ID unknown : " + req.params.id);

  try {
    await UserModel.findOneAndUpdate(
      { _id: req.params.id },
      {
        $set: {
          bio: req.body.bio,
        },
      },
      { new: true, upsert: true, setDefaultsOnInsert: true },
      (err, docs) => {
        if (!err) return res.send(docs);
        if (err) return res.status(500).send({ message: err });
      }
    );
  } catch (err) {
    return res.status(500).json({ message: err });
  }
};

module.exports.deleteUser = async (req, res) => {
  if (!ObjectID.isValid(req.params.id))
    return res.status(400).send("ID unknown : " + req.params.id);

  try {
    await UserModel.remove({ _id: req.params.id }).exec();
    res.status(200).json({ message: "Successfully deleted. " });
  } catch (err) {
    return res.status(500).json({ message: err });
  }
};

module.exports.follow = async (req, res) => {
  if (
    !ObjectID.isValid(req.params.id) ||
    !ObjectID.isValid(req.body.idToFollow)
  )
    return res.status(400).send("ID unknown : " + req.params.id);

  try {
    // add to the follower list
    await UserModel.findByIdAndUpdate(
      req.params.id,
      { $addToSet: { following: req.body.idToFollow } },
      { new: true, upsert: true },
      (err, docs) => {
        if (!err) res.status(201).json(docs);
        else return res.status(400).jsos(err);
      }
    );
    // add to following list
    await UserModel.findByIdAndUpdate(
      req.body.idToFollow,
      { $addToSet: { followers: req.params.id } },
      { new: true, upsert: true },
      (err, docs) => {
        // if (!err) res.status(201).json(docs);
        if (err) return res.status(400).jsos(err);
      }
    );
  } catch (err) {
    return res.status(500).json({ message: err });
  }
};

module.exports.unfollow = async (req, res) => {
  if (
    !ObjectID.isValid(req.params.id) ||
    !ObjectID.isValid(req.body.idToUnfollow)
  )
    return res.status(400).send("ID unknown : " + req.params.id);

  try {
    await UserModel.findByIdAndUpdate(
      req.params.id,
      { $pull: { following: req.body.idToUnfollow } },
      { new: true, upsert: true },
      (err, docs) => {
        if (!err) res.status(201).json(docs);
        else return res.status(400).jsos(err);
      }
    );
    // remove to following list
    await UserModel.findByIdAndUpdate(
      req.body.idToUnfollow,
      { $pull: { followers: req.params.id } },
      { new: true, upsert: true },
      (err, docs) => {
        // if (!err) res.status(201).json(docs);
        if (err) return res.status(400).jsos(err);
      }
    );
  } catch (err) {
    return res.status(500).json({ message: err });
  }
};
cnjp1d6j

cnjp1d6j1#

试试这个

module.exports.userInfo = async(req, res) => {
  if (!ObjectID.isValid(req.params.id))
    return res.status(400).send("ID unknown : " + req.params.id);

  try{
      const docs = await UserModel.findById(req.params.id).select("-password").exec();
      res.send(docs);

  }catch(err){
     console.log(err);
  }

};

相关问题