Cast to [ObjectId] failed for value

Issue

Please help me with this one. I have one post query and I want to save it into two tables seperately. But I encountered an error message from postman " Cast to [ObjectId] failed for value "
Please help me! Thank you!

Here is my code below:

FirstModel

const mongoose= require('mongoose');
const { Schema } = mongoose;
const class = require('./classModel');

const SubjectSchema = new mongoose.Schema({
name: {
    type: String,
    unique: true
},
address: {
    type: String,
},
classes: [ { type: Schema.Types.ObjectId, ref: class.schema } ]

})

Second Model

const classSchema = new mongoose.Schema({
name: {
    type: String,
    unique: true
},

})

and here is my controller

exports.create = catchAsync (async (req, res, next) => {

const newSubject = await Subject.create(req.body)

let payload = {
    body : {
        ...req.body.classes,
        _id: new mongoose.Types.ObjectId(),
        golf_course_id: newGolfCourse._id
    }
}


const newClass = await Class.createClass(payload)

if(!newClass ) {
    res.status(201).json({
        status: 'success',
        data : {
            subject: newSubject 
        }
    })
}

})

Solution

It’s because you are trying to set your classes in your Subject before they have ObjectIds. In your Subject Schema you have the following line:

classes: [ { type: Schema.Types.ObjectId, ref: class.schema } ]

Which tells Mongoose that you are referencing another model with it’s assigned ObjectId but in your case when you try to save it:

await Subject.create(req.body)

These classes don’t have ID’s yet, so you need to create them. I wouldn’t manually set them, when mongoose sets them on it’s own. So the following would be a suggestion.

try {
    const classes = req.body.classes.map(async (className) => {
      const newClass = new Class({ name: className });
      newClass.golf_course_id = newGolfCourse._id;
      await newClass.save();
      return newClass;
    });
    const results = await Promise.all(classes);

    const newSubject = await new Subject({
      name: req.body.name,
      address: req.body.address,
      classes: results,
    });

    res.send(newSubject);
  } catch (error) {
    console.log(error);
  }

This method creates the classes just like you had in your code but then it give the classes to the Subject after they have been created, that way the Subject can use them. Otherwise you will get an ObjectId error because you can’t assign a field with a reference e.g. classes: [ { type: Schema.Types.ObjectId, ref: class.schema } ] without a reference to that item. By the way in your ref: class.schema just use ref: 'Class'

Answered By – Dylan L.

Answer Checked By – Cary Denson (AngularFixing Admin)

Leave a Reply

Your email address will not be published.