Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use populate function inside a nested object

In my user schema, there is a field as such.

courseTaken: [
    {
      course: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Course",
      },
      rating: {
        type: Number,
        min: 0,
        max: 5,
        default: 0,
        validate: {
          validator: function (v) {
            return Number.isInteger(v * 2);
          },
          message: "Rating can only be incremented by 0.5",
        },
      },
    },
  ], 

I was doing a route where the user getss the courses he has taken. To do this I was using populate. I was doing something like this:

exports.getClassroom = catchAsync(async (req, res, next) => {
  let t = await req.user.courseTaken.populate("course");
  const classroom = t.courseTaken;
  console.log(t);
  res.status(200).json({
    status: "success",
    classroom,
  });
  next();
});

But, I get the following error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: req.user.courseTaken.populate is not a function
    at /Users/vedantamohapatra/msb-academy/api/controllers/userController.js:105:38
    at /Users/vedantamohapatra/msb-academy/api/utils/catchAsync.js:3:7
    at Layer.handle [as handle_request] (/Users/vedantamohapatra/msb-academy/api/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/vedantamohapatra/msb-academy/api/node_modules/express/lib/router/route.js:144:13)
    at /Users/vedantamohapatra/msb-academy/api/controllers/authController.js:95:3
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

>Solution :

The reason for the error is that you are trying to call the populate method on an array, which is not a Mongoose model.

you can use the populate method on the Mongoose model for the User schema, and specify the path option to populate the courseTaken field.

You can try this code:

exports.getClassroom = catchAsync(async (req, res, next) => {
  const user = await User.findById(req.user._id).populate('courseTaken.course');
  const classroom = user.courseTaken;
  res.status(200).json({
    status: "success",
    classroom,
  });
  next();
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading