mongoose 尝试填充数据但未填充

uqcuzwp8  于 2022-11-13  发布在  Go
关注(0)|答案(1)|浏览(162)

我有两个模型user-rolescandidate-details,我想将user-roles集合id作为candidateId填充到candidate-details
我的用户角色模型

const userRolesSchema=mongoose.Schema({
    phoneNumber:{
        type:Number,
        unique:false,
        validate(value){
            if(value.toString().length !== 10){
                throw new Error('Phone number should be of 10 digits');
            }
        },
    },
    isCandidate:{
        type:Boolean
    },
    isCompany:{
        type:Boolean
    }
})

我的候选人详细信息模型

const candidateRegisterationSchema=new Schema({
    firstName:{
        type:String,
        required:true
    },
    lastName:{
        type:String,
        required:true
    },
    userName:{
        type:String,
        required:true,
        unique:true
    },
    phoneNumber:{
        type:Number,
        unique:true,
        required:true,
        validate(value){
            if(value.toString().length !== 10){
                throw new Error('Phone number should be of 10 digits');
            }
        }
    },
    whatsappNumber:{
        type:Number,
        unique:true,
        required:true,
        validate(value){
            if(value.toString().length !== 10){
                throw new Error('Phone number should be of 10 digits');
            }
        },
    },
    candidateId:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"user-roles",
        required:true
    }
})

我的候选人详细信息控制器

const candidateRegisteration = async (req, res) => {
  const candidate = new CandidateRegisterationModel();
  const user = await UserRolesModel.findOne({ _id: req.params.c_id }).populate({path:'candidateId',strictPopulate: false });
  if (user !== null) {
    try {
      candidate.firstName = req.body.firstName;
      candidate.lastName = req.body.lastName;
      candidate.userName = req.body.userName;
      // candidate.candidateId = user._id;
      candidate.whatsappNumber = req.body.whatsappNumber;
      candidate.phoneNumber = user.phoneNumber;

      await candidate.save();
      res.status(201).json({
        message: "candidate saved successfully",
        statusCode: res.statusCode,
      });
    } catch (error) {
      console.log(error);
      res.status(500).json({ error, statusCode: res.statusCode });
    }
  }
  else{
    res.status(404).json({
      message:"candidate not found",
      statusCode:res.statusCode
    })
  }
};

我是一个新手,我需要你的帮助来理解和修复这个填充方法请,我试图发送请求获得错误一样

name": "ValidatorError",
                "message": "Path `candidateId` is required.",

这意味着它不会填充candidateId中的id

p8h8hvxi

p8h8hvxi1#

const candidateRegisteration = (req, res) => {
  try {
    const user = await UserRolesModel.findOne({
      _id: req.params.c_id
    });
    if (user) {
      const {
        firstName,
        lastName,
        userName,
        whatsappNumber
      } = req.body

      const {
        _id: candidateId,
        phoneNumber
      } = user;

      const candidate = new CandidateRegisterationModel({
        firstName,
        lastName,
        userName,
        whatsappNumber,
        candidateId,
        phoneNumber
      });

      await candidate.save();

      res.status(201).json({
        message: "candidate saved successfully",
        statusCode: res.statusCode,
      });

    } else {
      res.status(404).json({
        message: "candidate not found",
        statusCode: res.statusCode
      })
    }
  } catch (error) {
    console.log(error);
    res.status(500).json({
      error,
      statusCode: res.statusCode
    });
  };
}

相关问题