Cast to ObjectId failed for value \"aggregate\" at path \"_id\" for model Payment\"
See original GitHub issueDo you want to request a feature or report a bug? bug
What is the current behavior? Cast to ObjectId failed for value emitting
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior? It should accept the ref key as String
Please mention your node.js, mongoose and MongoDB version. NODE: 10.0.0, “mongoose”: “^5.0.0-rc0” v4.0.0
let mongoose = require("mongoose");
let Schema = mongoose.Schema;
let ObjectId = Schema.ObjectId;
let modelSchema = new Schema(
{
userId: {type: ObjectId},
name: { type: String },
amount: {
type: Number
},
categoryId: {
type: String,
ref: 'Category'
},
cardId: {
type: String,
ref: 'Card'
},
paymentDate: {
type: Date,
default: new Date
}
},
{
timestamps: {}
}
);
let modelObj = mongoose.model("Payment", modelSchema);
module.exports = modelObj;
Issue Analytics
- State:
- Created 5 years ago
- Comments:6
Top 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 >[SOLVED] CastError: Cast to ObjectId failed for value "{ articleId
When I click My Article button from the homepage I get the following error : CastError: Cast to ObjectId failed for value “{...
Read more >Mongoose v6.8.2: API docs
isValidObjectId (); Mongoose.prototype.model(); Mongoose.prototype. ... path «String» The path a.b.c in the doc where this cast error occurred.
Read more >Cast to ObjectId failed for value - Code Grepper
// it happens when you pass an invalid id to mongoose. 2. // so first check it before proceeding, using mongoose isValid function....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@techyaura you can create a virtual on paymentSchema that defines specific local/foreign keys.
Here is the example from earlier updated with virtuals instead:
6879.js
Output:
@techyaura I’m assuming you are calling something like
Payment.findOne().populate('categoryId')
.Mongoose is going to use the Model pointed to by the value of
ref
and the_id
field of the docs in the corresponding collection to match the value of the path in the document being populated.Your _id fields from the ‘Card’ and ‘Category’ schema are using the default of
ObjectId
. A string like ‘aggregate’ is never going to successfully be cast as anObjectId
.You need to store the
_id
of theCard
orCategory
document in that field. If you store the _id of the foreign doc, it is fine to use String as the type.Here’s a complete repro script based on your schema:
6879.js
Output: