Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Python
  3. Question
Python

How to resolve mongoose casterror to object id?

Asked by Dia Shrinidhi Jul 27, 2023 2.2K views 1 answer
Share

About this question

 I am a novice to node.js. I am aiming to make a path for creating a child object, include it to the parent’s range of children, and return the child object to the requester. However, on passing the string id into findByld, node collides with

TypeError : Object {} has no method ‘cast’
On passing in an Obejctid, there’s an error
The code is depicted below:
Var mongoose = require (‘mongoose’);
Car schema = mongoose.schema;
Var objectId = schema.ObjectId;
// have also tried schema.Types.ObjectId, mongoose.ObjectId
Mongoose.connect (‘mongodb://user:password@server :port/database’0;
app.get9’/myclass/; Id/childClass/create’, function(request, result) {
Var id = new objectId (request.params.Id); Myclass.findByid(id).exec ( functions
If (err || !myclass) {
result.send(“ error: “ + err + “
“ +
JSON.stringify(id) || (“object ‘“ + request.params.Id
“‘ not found : “ + id));
 Return;
}
Var child = childclass();
myclass.children.addToset(child);
myclass.save();result. send(child);
});
})/
I attempted to deploy the findone and passing in {_id:id}. I also attempted the various classes for ObjectId.
CastError : Cast to objectid failed for value “ [object Obejct]” at path “ _id”

Your answer

1 Answer

More Python discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Python Blogs

Guides, tips and career advice on Python from JanBask experts.