简体   繁体   English

Mongoose findOne 在类型 mongoose.Schema.Types.ObjectId 上返回 null

[英]Mongoose findOne returns null on type mongoose.Schema.Types.ObjectId

I have a Model that contains a foreign key eid of type mongoose.Schema.Types.ObjectId .我有一个模型,其中包含mongoose.Schema.Types.ObjectId类型的外键eid Whenever I try to filter records using that particular column i get a null result yet the record exists.每当我尝试使用该特定列过滤记录时,我得到一个空结果但记录存在。 Here is the schema.这是架构。

const attendanceSchema = mongoose.Schema(
  {
    eid: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Employee",
      required: true,
    },
    month: {
      type: String,
      required: true,
    },
    paidAmount: {
      type: Number,
      default: 0,
    },
    yearMonth: {
      type: Number,
      required: true,
    },
  },
  {
    timestamps: true,
  }
);

here is the query这是查询

  const attendance = await Attendance.findOne({
      eid:req.body.eid,
    });

I also tried casting eid from req.body to mongoose.Schema.Types.ObjectId我还尝试将 eid 从 req.body 转换为mongoose.Schema.Types.ObjectId

 const attendance = await Attendance.findOne({
      eid: mongoose.Types.ObjectId(req.body.eid),
    });

I was able to solve the problem.我能够解决这个问题。 I Found out that I used req.body.eid rather than req.body.employeeId .我发现我使用req.body.eid而不是req.body.employeeId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM