-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CastError doesn't return the entire nested path #14114
Comments
Am able to get pathing for the errors. Please modify script below to demonstrate your issue. const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
id: mongoose.Schema.Types.ObjectId,
name: String,
accessories: [
{
isEnabled: Boolean,
additionals: [
{
k: String,
v: Number,
},
],
}
],
actions: [
{
type: String,
isEnabled: Boolean,
subActions: [
{
type: String,
isEnabled: Boolean,
}
],
},
],
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const obj = {
// id: '123456789012',
name: 'Soldier',
accessories: [
{
isEnabled: true,
additionals: [
{
k: ['helmet'],
v: 1
},
{
k: 'shoes',
v: 2
}
]
}
],
actions: [
{
type: 'move',
isEnabled: true,
subActions: [
{ type: 'move right',
isEnabled: true
}
]
}
]
}
await Test.create(obj);
console.log(await Test.findOne())
console.log('done');
}
run(); |
Hello thanks to have experimented, the problem seems to only appear on CastError and I can't manage to produce a CastError on create, we only have ValidationError. const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
id: mongoose.Schema.Types.ObjectId,
name: String,
accessories: [
{
isEnabled: Boolean,
additionals: [
{
k: String,
v: Number,
},
],
}
],
actions: [
{
type: {type:String },
isEnabled: Boolean,
subActions: [
{
type: {type:String },
isEnabled: Boolean,
}
],
},
],
});
const Test = mongoose.model('Test', testSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
const obj = {
// id: '123456789012',
name: 'Soldier',
accessories: [
{
isEnabled: true,
additionals: [
{
k: 'test',
v: 1
},
{
k: 'shoes',
v: 2
}
]
}
],
actions: [
{
type: 'move',
isEnabled: true,
subActions: [
{ type: 'move right',
isEnabled: true
}
]
}
]
}
let soldier = await Test.create(obj);
await Test.findOneAndUpdate({_id : soldier._id}, {
"$set": {
"accessories.0.additionals.0.k": ['test']
}
})
console.log('done');
}
run(); |
Not able to reproduce but from the error message you had copy-pasted it seems like an object was passed and that triggered the cast error as opposed to the array of strings.
|
Hello, thank you for your time. |
fix(update): set CastError path to full path if casting update fails
Prerequisites
Mongoose version
8.0.0
Node.js version
20.9.0
MongoDB version
6.2.0
Operating system
None
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
Hello,
I'm testing Mongoose error feedback. When testing a ValidationError, the response returns a nested path, which allows me to trace the field in error in my object that I passed in my query.
Example:
'accessories.0.additionals.0.k'
.It's very useful for browsing and finding fields in error.
However, when testing a CastError, the return sends me the error by browsing the object. This means I can't go back down into my object to find my error correctly.
Example: for arrays, the index isn't specified, so it's impossible to retrieve it.
Schema :
Example of a body sent :
And the server returns this CastError :
Example of a ValidationError :
The text was updated successfully, but these errors were encountered: