Mongoose rejects an object you push into an array field
CastError: Cast to string failed for valueShort answer
Mongoose casts every pushed value to the type the schema declares. If the array is declared as [String], an object cannot be cast and the push fails with a CastError. Declare the array with a sub-schema so it becomes a document array. Also check for a key named type inside a nested object, which Mongoose reads as a field type.
Fix it fast
1Find out which cause you have
Print how Mongoose sees the field. If the type is String, or the array holds Strings, the schema is the problem, not the data you send.
console.log(Order.schema.path('items'));2Match what you see, then jump to its fix
- 1
Cast to string failed ... at path "items" and the schema line reads items: [String] or items: [{ type: String }]The array is declared as [String] but you push objectsFix - 2
The failing field is an object with a property called type, and Mongoose treats the whole path as a StringA nested object uses the key type, so Mongoose reads it as a typeFix
- 1
What does the error look like?
CastError: Cast to string failed for valueCast to string failed for value ... at path "items"
Cause 1 of 2
The array is declared as [String] but you push objects
1Check
Open the schema. A list of SchemaTypes such as [String] is a primitive array, and each element is cast to that type. An object cannot become a string, so the cast fails.
grep -n "items" models/Order.js2Fix
Describe the element as a sub-schema, so the field becomes a document array that accepts objects with those keys.
const ItemSchema = new mongoose.Schema({ name: String, qty: Number });
const OrderSchema = new mongoose.Schema({
items: [ItemSchema], // document array
});3Verify
Push an object and save. It should save with no CastError.
const order = await Order.create({});
order.items.push({ name: 'milk', qty: 2 });
await order.save();
console.log(order.items[0].name);Confirmed by mongoosejs.com
Cause 2 of 2
A nested object uses the key type, so Mongoose reads it as a type
1Check
Mongoose treats type as a special property. In an object like { type: String, ticker: String } the path is read as one String, so pushing or setting an object fails to cast.
grep -n "type:" models/*.js2Fix
Nest the real field named type inside its own type declaration, as the Mongoose docs show.
const holdingSchema = new mongoose.Schema({
asset: {
type: { type: String },
ticker: String,
},
});3Verify
Create a document with the nested object and confirm it saves without a CastError.
await Holding.create({ asset: { type: 'stock', ticker: 'ABC' } });Confirmed by mongoosejs.com
How do I stop it coming back?
- Use a sub-schema for any array that holds objects; keep [String] and [Number] for plain values.
- Avoid naming a field type. If you must, use the type: { type: String } form.
- Validate req.body shape before pushing it into a document.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fixBack end
Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The Network tab shows the asset URL answered with Content-Type text/html
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type.
3 causes, each with a fix