-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Pushing a discriminated object in a non-discriminated array will strip its special properties #2399
Comments
Interesting. I'll take a look at this when I can. |
@anoirclere |
@alabid Yes in my case myThingContainer.things is populated. Do you mean push() is not supposed to work with populated arrays? Here's my code. A Building is a specialized Group, and a Residence contains an array of Group objects. /*
* Add building
*/
router.post('/', function(req, res, next) {
var building = new Building({
name: req.body.newBuildingName,
type: req.body.newBuildingType,
residence: req.residence._id,
});
building.save(function (err, building) {
if(err) return next(err);
//req.residence.groups.push(building);
// The commented line above doesn't work since Mongoose will strip property '__t' from Building object at push.
// Workaround : don't use rewritten "push" method.
req.residence.groups = req.residence.groups.concat([building]);
req.residence.groups.sort(function(a, b) {
return (a.name < b.name) ? -1 : 1;
});
req.residence.save(function (err, residence) {
if(err) return next(err);
res.render('shared/_edit_buildings');
});
});
}); |
gh-2399 prevent casting for discriminator models when pushing to array
Hello,
I ran against this issue today.
I have these models :
Now, I have a ThingContainer and I want to add a SpecialThing to its array of Things. The issue is that the push method will cast the SpecialThing object and strip its "specialAttribute" as well as Mongoose's discriminating attribute "__t".
My current workaround is to avoid the push method :
I wonder if the push method should be aware of discriminated objects attributes ?
The text was updated successfully, but these errors were encountered: