Mongoose: CastError: Cast to ObjectId failed for value “[object Object]” at path “_id”

前端 未结 13 1977
再見小時候
再見小時候 2020-11-30 01:24

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\'

相关标签:
13条回答
  • 2020-11-30 02:17

    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.

    0 讨论(0)
  • 2020-11-30 02:17

    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!

    0 讨论(0)
  • 2020-11-30 02:17

    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(),
    
    0 讨论(0)
  • 2020-11-30 02:18

    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.

    0 讨论(0)
  • 2020-11-30 02:18

    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

    0 讨论(0)
  • 2020-11-30 02:20

    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!

    0 讨论(0)
提交回复
热议问题