-
-
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
Subdocument Array of Objects Schema causes "Cast to array failed for value" validation error when you use ...
spread operator versus toObject() for inheritance
#11522
Comments
Note this only happens when you don't use |
toObject is basically transforming the hydrated Document to a lean Object. Probably when casting we have to check if it is a hydrated Document and lean it. |
I'll try to repro this. However, in general, I'd recommend using |
Confirmed and fixed. Although I still would strongly recommend doing the below instead: for (const member of ctx.state.domain.members) {
member.group = member.user.toString() === ctx.params.member_id
? ctx.request.body.group
: member.group;
} You're not writing React here - by shallow cloning the array and all the documents you're incurring an unnecessary performance penalty and making your code more complex. |
@vkarpov15 this rewrite to use the loop will throw the following error for (const member of ctx.state.domain.members) {
if (member.user.toString() === ctx.params.member_id)
member.group = ctx.request.body.group;
} |
@vkarpov15 we've confirmed that the fix is the following: // swap the user group based off ctx.request.body.group
ctx.state.domain.members = ctx.state.domain.members.map((member) => {
return {
user: member.user,
group:
member.user.toString() === ctx.params.member_id
? ctx.request.body.group
: member.group
};
}); |
… added ansible verbosity, bump deps
@titanism I'm trying to figure out why your original example doesn't work. Looking at forwardemail/forwardemail.net@9e3ec85#diff-93a5248c3ab689a90e0344028135658b7cf18f44872ed83ecc329f9e912342c9, why is the original code: for (const member of domain.members) {
if (member.user.toString() === ctx.params.member_id)
member.group = ctx.request.body.group;
} and not the below? Loop should be over for (const member of ctx.state.domain.members) {
if (member.user.toString() === ctx.params.member_id)
member.group = ctx.request.body.group;
} |
… added ansible verbosity, bump deps
Hey @vkarpov15 - hope all is well.
I'm writing as this was a frustrating bug to track down. I believe this worked in version 5 of Mongoose, but I could be wrong and this totally has never worked since I switched to use
...
spread operator here for inheriting the object.Here's the diff showing the fix I had to do to avoid the issue:
The schema is defined here https://github.com/forwardemail/forwardemail.net/blob/master/app/models/domain.js#L131 and here https://github.com/forwardemail/forwardemail.net/blob/master/app/models/domain.js#L45-L58 (pretty simple).
The text was updated successfully, but these errors were encountered: