Trying to populate data but not populating

I have two models user-roles and candidate-details , I want to populate user-roles collection id as candidateId in candidate-details

My user-roles model

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
    }
})

My candidate Detail Model

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
    }
})

my candidate deatils controller

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
    })
  }
};

I am a newbie and I need your help to understand and fix this populate method please ,I tried sending request getting error like

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

that means its not populating the id in candidateId

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