为什么我会得到';对于值';转换为ObjectId失败;发送网络请求时mongodb出错?

为什么我会得到';对于值';转换为ObjectId失败;发送网络请求时mongodb出错?,mongodb,mongoose,mongodb-query,social-networking,mern,Mongodb,Mongoose,Mongodb Query,Social Networking,Mern,我正试图建立一个社交网站,在那里我想整合追随者的建议。因此,我向后端发送了带有用户id数组的网络请求 backend request.body包含此数组“以下内容”:[“608b05477eeba243c5ac8bcb”,“608b05477eeba243c5ac8bcc”] 我需要数据库中的所有用户,除了以下数组中的id。我已在后端编写了此查询 userRouter.post( "/suggestions", expressAsyncHandler(async (r

我正试图建立一个社交网站,在那里我想整合追随者的建议。因此,我向后端发送了带有用户id数组的网络请求

backend request.body包含此数组
“以下内容”:[“608b05477eeba243c5ac8bcb”,“608b05477eeba243c5ac8bcc”]

我需要数据库中的所有用户,除了以下数组中的id。我已在后端编写了此查询

userRouter.post(
  "/suggestions",
  expressAsyncHandler(async (req, res) => {
    console.log(req.body.following);
    const suggestedUsers = await User.find({
      _id: { $ne: req.body.following },
    });

    res.send(suggestedUsers);
  })
);
但是每当我从邮递员发送请求时,我都会收到这个错误

{
    "message": "Cast to ObjectId failed for value \"[ '608b05477eeba243c5ac8bcb', '608b05477eeba243c5ac8bcc' ]\" at path \"_id\" for model \"User\""
}
我的userModel.js如下所示

const userSchema = new mongoose.Schema(
  {
    username: { type: String, required: true },
    fullName: { type: String, required: true },
    emailAddress: { type: String, required: true },
    password: { type: String, required: true },
    following: [{ type: mongoose.Schema.Types.ObjectId, required: true }],
    followers: [{ type: mongoose.Schema.Types.ObjectId, required: true }],
  },
  {
    timestamps: true,
  }
);

const User = mongoose.model("User", userSchema);

export default User;
这是我的用户数据库记录
您似乎正在尝试检查它是否不在ID数组中,因此您应该使用
$nin
(不在)而不是
$ne
(不相等)


这真的很有效,谢谢你,伙计
_id: { $nin: req.body.following },