100

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

mongoose.connect('mongodb://user:password@server:port/database');

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)); return; }
    var child = ChildClass();
    myClass.Children.addToSet(child);
    myClass.save();
    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, like 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 feedback.

EDIT:

After the workaround from Peter Lyons, I googled another error that I was running into and found findByIdAndUpdate, which works as expected and does exactly what I was hoping to do. I'm still not sure why findById and findOne were giving me such issues and I'm curious to know (maybe a bug report needs to be filed), so I'll leave this open in case someone else has an answer.

jmblackmer
  • 1,217
  • 2
  • 8
  • 11
  • My guess is your were calling `findOne` with a single `ObjectId` but `findOne` expects a regular JS Object of key/value conditions. – Peter Lyons Aug 13 '13 at 15:15
  • I have answered these question [here](https://stackoverflow.com/questions/41567319/getting-error-in-mongodb-cast-issue-for-valid-object-id/53139672#53139672). – byal Nov 04 '18 at 10:13
  • 2
    Check your routes. It might be that two routes have the same starting point example "#GET /user/:userID and #GET /user/list". The route will use "list" as an input to the database and will give some error. – Jose Hernandez Jan 26 '20 at 23:26

19 Answers19

90

Short answer: use mongoose.Types.ObjectId.

Mongoose (but not mongo) can accept object Ids as strings and "cast" them properly for you, so just use:

MyClass.findById(req.params.id)

However, the caveat is if req.params.id is not a valid format for a mongo ID string, that will throw an exception which you must catch.

So the main confusing thing to understand is that mongoose.SchemaTypes has stuff you only use when defining mongoose schemas, and mongoose.Types has the stuff you use when creating data objects you want to store in the database or query objects. So mongoose.Types.ObjectId("51bb793aca2ab77a3200000d") works, will give you an object you can store in the database or use in queries, and will throw an exception if given an invalid ID string.

findOne takes a query object and passes a single model instance to the callback. And findById is literally a wrapper of findOne({_id: id}) (see source code here). Just find takes a query object and passes an array of matching model instances to the callback.

Just go slow. It's confusing but I can guarantee you you are getting confused and not hitting bugs in mongoose at this point. It's a pretty mature library, but it takes some time to get the hang of it.

The other suspect thing I see in your snippet is not using new when instantiating ChildClass. Beyond that, you'll need to post your schema code in order for us to help you tract down any CastErrors that remain.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • If I try to use "mongoose.Types.ObjectId("51c35e5ced18cb901d000001");" (by placing it directly in code), I still get the error "TypeError: Object {} has no method 'cast'". I also tried instantiating it as an object and got the same error. – jmblackmer Jun 20 '13 at 21:10
  • Upgrade to the latest mongoose? I'm on 3.6.11. – Peter Lyons Jun 20 '13 at 21:20
  • Calling MyClass.find(req.params.id) works. Shouldn't findOne and findById work the same way? It just seems a bit counter-intuitive to have find be the function that returns a single document when the other two exist. I've altered the code to get the first item of the array now, but it is failing on addToSet() now with "TypeError: Object {} has no method 'cast'". – jmblackmer Jun 20 '13 at 21:27
  • I'm also on 3.6.11. I just set all of this up the other night, so I'd expect that everything is already up to date. – jmblackmer Jun 20 '13 at 21:28
  • Does not work as of July 3 2014!! Why `new mongoose.Schema.ObjectId("51bb793aca2ab77a3200000d")` works fine, but `new mongoose.Schema.ObjectId(request.params[0])` throws an error?? Whereby I place a string `"51bb793aca2ab77a3200000d"` in a url `/51bb793aca2ab77a3200000d`. And when obtained from a url it throws an error. – Green Jul 03 '14 at 10:19
  • 1
    You are using the wrong constructor function. Use `mongoose.Types.ObjectId`. `mongoose.Schema.ObjectId` is for defining schemas only. – Peter Lyons Jul 03 '14 at 14:44
59

I've faced this error, That was because the value you want to filter in the _id field is not in an ID format, one "if" should solve your error.

const mongoose = require('mongoose');

console.log(mongoose.Types.ObjectId.isValid('53cb6b9b4f4ddef1ad47f943'));
// true
console.log(mongoose.Types.ObjectId.isValid('whatever'));
// false

To solve it, always validate if the criteria value for search is a valid ObjectId

const criteria = {};
criteria.$or = [];

if(params.q) {
  if(mongoose.Types.ObjectId.isValid(params.id)) {
    criteria.$or.push({ _id: params.q })
  }
  criteria.$or.push({ name: { $regex: params.q, $options: 'i' }})
  criteria.$or.push({ email: { $regex: params.q, $options: 'i' }})
  criteria.$or.push({ password: { $regex: params.q, $options: 'i' }})
}

return UserModule.find(criteria).exec(() => {
  // do stuff
})
gsalgadotoledo
  • 2,666
  • 1
  • 25
  • 22
  • 6
    Correct. I was testing an endpoint to `GET` user with a `findById`, turns out that the id param must be 12 bits, otherwise mongoose will fail to cast. I was using random short numbers. – 10110 Apr 18 '19 at 20:56
5

For all those people stuck with this problem, but still couldn't solve it: I stumbled upon the same error and found the _id field being empty.

I described it here in more detail. Still have not found a solution except changing the fields in _id to not-ID fields which is a dirty hack to me. I'm probably going to file a bug report for mongoose. Any help would be appreciated!

Edit: I updated my thread. I filed a ticket and they confirmed the missing _id problem. It is going to be fixed in the 4.x.x version which has a release candidate available right now. The rc is not recommended for productive use!

Community
  • 1
  • 1
Stefan Medack
  • 2,731
  • 26
  • 32
4

If you are having this issue and you are performing a populate somewhere along the lines, see this Mongoose issue.

Update to Mongoose 4.0 and the issue has been fixed.

ac360
  • 7,735
  • 13
  • 52
  • 91
2

Had the same problem, I just coerced the id into a string.

My schema:

const product = new mongooseClient.Schema({
    retailerID: { type: mongoose.SchemaTypes.ObjectId, required: true, index: true }
});

And then, when inserting:

retailerID: `${retailer._id}`
camstuart
  • 623
  • 3
  • 13
2

I want data from all docs, and I don't want _id, so I do:

User.find({}, {_id:0, keyToShow:1, keyToNotShow:0})
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
jazeb007
  • 578
  • 1
  • 5
  • 11
2

I had the same problem, turned out after I have updated my schema, I have forgotten I was calling the model using the old id, which was created by me; I have updated my schema from something like:

patientid: {
           type: String,
            required: true,
            unique: true
          }, 

to

patientid: { type: mongoose.SchemaTypes.ObjectId, ref: "Patient" },

It turned out, since my code is big, I was calling the findOne with the old id, therefore, the problem.

I am posting here just to help somebody else: please, check your code for unknown wrong calls! it may be the problem, and it can save your huge headacles!

  • Hi there! This one was the solution to my problem too! I had to insert new data on the new model for it work. Have a great day! – destroyer22719 Apr 02 '21 at 20:52
1

I was receiving this error CastError: Cast to ObjectId failed for value “[object Object]” at path “_id” after creating a schema, then modifying it and couldn't track it down. I deleted all the documents in the collection and I could add 1 object but not a second. I ended up deleting the collection in Mongo and that worked as Mongoose recreated the collection.

Enkode
  • 4,515
  • 4
  • 35
  • 50
1

For the record: I had this error trying to fill a subdocument in a wrong way:

{
[CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"]
message: 'Cast to ObjectId failed for value "[object Object]" at path "_id"',
name: 'CastError',
type: 'ObjectId',
path: '_id'
value:
  [ { timestamp: '2014-07-03T00:23:45-04:00',
  date_start: '2014-07-03T00:23:45-04:00',
  date_end: '2014-07-03T00:23:45-04:00',
  operation: 'Deactivation' } ],
}

look ^ value is an array containing an object: wrong!

Explanation: I was sending data from php to a node.js API in this way:

$history = json_encode(
array(
  array(
  'timestamp'  => date('c', time()),
  'date_start' => date('c', time()),
  'date_end'   => date('c', time()),
  'operation'  => 'Deactivation'
)));

As you can see $history is an array containing an array. That's why mongoose try to fill _id (or any other field) with an array instead than a Scheme.ObjectId (or any other data type). The following works:

$history = json_encode(
array(
  'timestamp'  => date('c', time()),
  'date_start' => date('c', time()),
  'date_end'   => date('c', time()),
  'operation'  => 'Deactivation'
));
Igor Parra
  • 10,214
  • 10
  • 69
  • 101
1

I also encountered this mongoose error

CastError: Cast to ObjectId failed for value \"583fe2c488cf652d4c6b45d1\" at path \"_id\" for model User

So I run npm list command to verify the mongodb and mongoose version in my local. Heres the report:

......  
......  
├── mongodb@2.2.19  
├── mongoose@4.7.2  
..... 

It seems there's an issue on this mongodb version so what I did is I uninstall and try to use different version such as 2.2.16

$ npm uninstall mongodb, it will delete the mongodb from your node_modules directory. After that install the lower version of mongodb.
$ npm install mongodb@2.2.16
Finally, I restart the app and the CastError is gone!!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Arman Ortega
  • 3,003
  • 1
  • 30
  • 28
1

I am not sure this will help but I resolved the issue by importing mongoose like below and implementing it as below

const mongoose = require('mongoose')

_id: new mongoose.Types.ObjectId(),
David Buck
  • 3,752
  • 35
  • 31
  • 35
Kingsley Akpan
  • 213
  • 3
  • 3
1

so in my case , i used the put request from a doc and i request to api file and what i did is i send the "id" wrongly through the url this was mistake "/api/posts/${postId}/like" but changing the double quotations to backticks changed the game ,/api/posts/${postId}/like and in the api file when i use the findbyid(postid) , i got the resut without error

heisenberg
  • 13
  • 3
0

I was having the same problem.Turns out my Node.js was outdated. After upgrading it's working.

Rajesh Barik
  • 183
  • 2
  • 3
  • 12
0

just change the path it will work for example

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

change to

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

I just added --es-- to the path (myClass) to become (myClasses)

now should work and will not see that error

atline
  • 28,355
  • 16
  • 77
  • 113
0

For me, the ID was undefined (req.params.id returns undefined)

Zeyad Shaban
  • 896
  • 1
  • 8
  • 23
0

If you're finding a document by its "_id" for a model, say Drivers, the command below works quite well:

....
const driver = await Drivers.findById(<your id>);
....

Ensure you use the async-await and try-catch ES6 syntax

Chukwunazaekpere
  • 906
  • 1
  • 6
  • 13
0

For me, I was using a put request without sending any data. I changed it to a post request and it worked.

This thread covers more about put requests.

Zack Retro
  • 11
  • 4
0

Use:

MyClass.findOne(id)

Instead of:

MyClass.findById(id)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0
userRouter.get("/:id/:walletAddress", getDefaultWallet);
userRouter.get("/filterUser/:name", filterUserListByName);

First, I was using this, I got the above error then I change the position it works for me. Keep all routers above to "/: id/XYZ"

userRouter.get("/filterUser/:name", filterUserListByName);
userRouter.get("/:id/:walletAddress", getDefaultWallet);
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 22 '23 at 12:25
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34427490) – Wahlstrommm May 23 '23 at 13:16