Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
Cast to [ObjectId] failed for value

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

})

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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'

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!