question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CastError: Cast to ObjectId failed for value "XXX" at path "_id" for model "XXX"

See original GitHub issue

Do you want to request a feature or report a bug? Requesting a feature

What is the current behavior? _id is only accepting ObjectId even if we override it with string

What is the expected behavior? It need _id to accept strings rather than ObjectId

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. String in _id is supporting in Mongoose version - 5.12.6, which we are using. But it is not supported in Mongoose 5.12.7

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

9reactions
IslandRhythmscommented, May 7, 2021

The problem is that its not a valid _id string. It has to be either 12 byte binary string, or a 24 hex byte string. ejeLoP_FH1620208025781 is neither. Using this function should help https://mongoosejs.com/docs/api/mongoose.html#mongoose_Mongoose-isValidObjectId

1reaction
ramya-Senthamaraikannancommented, May 6, 2021

Issue 1: Sample data: { “_id” : “ejeLoP_FH1620208025781”, firstName: “xxxx”, lastName : “yyyy” }

after installing the latest version, any updates to this collection throws an error “CastError: Cast to ObjectId failed for value “XXX” at path “_id” for model “XXX””

Issue 2: Sample data: const mongoose = require(“mongoose”); const schema = new mongoose.Schema({ _id:String, firstName: String, lastName: String });

Inserting a new Record, the result is { _id : ObjectId(“609269995b2e888426d019ef”), firstName: “xxxx”, lastName : “xxxx” }

But the expected result is, { _id : “609269995b2e888426d019ef”, firstName: “xxxx”, lastName : “xxxx” }

we have our own logic to generate the _id as a String, which is not happening. Instead, it is creating its own ObjectId. This is only happening after the latest release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose: CastError: Cast to ObjectId failed for value "[object ...
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:...
Read more >
cast to objectid failed for value "" (type string) at path "_id" for ...
Mongoose's findById method casts the id parameter to the type of the model's _id field so that it can properly query for the...
Read more >
What's Mongoose error Cast to ObjectId failed for value XXX at ...
Node.js – Mongoose: CastError: Cast to ObjectId failed for value “[object Object]” at path “_id”. Short answer: use mongoose.Types.ObjectId.
Read more >
what's mongoose error cast to objectid failed for value yyy at ...
Controller.prototype.show = function(id, res) { this.model.findById(id, function(err, ... CastError: Cast to ObjectId failed for value "foo" at path "_id".
Read more >
FreeCodeCamp/HelpBackEnd - Gitter
findOne({ 'github.id': profile.id }, function (err, user) { if (err) { return ... /whats-mongoose-error-cast-to-objectid-failed-for-value-xxx-at-path-id.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found