Explore Courses Blog Tutorials Interview Questions
0 votes
in Web Technology by (47.6k points)

I am new to node.js, so I have a feeling that this will be something silly that I have overlooked, but I haven't been able to find an answer that fixes my problem. What I'm trying to do is create a path that will create a new child object, add it to the parent's array of children, then return the child object to the requester. The problem that I am running into is that if I pass the string id into findById, node crashes with

TypeError: Object {} has no method 'cast'

If I try to pass in an ObjectId instead, I get

CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"

Here is a rough outline of my code:

var mongoose = require('mongoose'); 

var Schema = mongoose.Schema; 

var ObjectId = Schema.ObjectId; 

//Have also tried Schema.Types.ObjectId, mongoose.ObjectId 


app.get('/myClass/:Id/childClass/create', function(request, result) { 

var id = new ObjectId(request.params.Id); MyClass.findById(id).exec( function(err, myClass) { 

if (err || !myClass) { 

result.send("error: " + err + "<br>" +

JSON.stringify(id) || ("object '" + request.params.Id

+ "' not found: " + id)); 


var child = ChildClass(); 

myClass.Children.addToSet(child);; result.send(child); 



If I execute this code with the path "/myClass/51c35e5ced18cb901d000001/childClass/create", this is the output of the code:

error: CastError: Cast to ObjectId failed for value "[object Object]" at path "_id" {"path":"51c35e5ced18cb901d000001","instance":"ObjectID","validators":[],"setters":[],"getters":[],"_index":null}

I've tried using findOne and passing in {_id:id} instead, but this appears to be exactly what findById does. I've tried the different classes for ObjectId that I've seen listed on other sites. I've tried calling ObjectId() like a function instead of a constructor and that returns undefined. At this point, I'm running out of ideas and it doesn't seem that googling for an answer is helping. Any ideas on what I'm doing wrong?

Also, as I said, I'm new to Node/Mongo/Mongoose/Express, so if there is a better way to accomplish my goal, please let me know. I appreciate all the feedback.

2 Answers

0 votes
by (106k points)

You can use the below-mentioned code I also had the same problem and it got resolved when I used it 

My schema:

const product = new mongooseClient.Schema({ 

retailerID: { type: mongoose.SchemaTypes.ObjectId,

required: true, index: true } 


And then, when inserting:

retailerID: `${retailer._id}`

0 votes
by (50.2k points)

For this, you can use mongoose.Types.ObjectId.

Mongoose can accept object Ids as strings and "cast" them properly for you, thus just use the following syntax:


Related questions

0 votes
0 answers
asked Jun 24, 2021 by Romainp92 (120 points)
+1 vote
2 answers
0 votes
1 answer
asked Sep 6, 2019 in SQL by Sammy (47.6k points)
0 votes
1 answer
asked Jan 5, 2020 in Web Technology by ashely (50.2k points)

Browse Categories