I have a POST route in an express app that aims to update a mongoose model based on form input. The "Base" model is Profile
, and I have two discriminator models Helper
and Finder
that conditionally add fields to the Profile
schema.
Thus, req.body.profile
will contain different fields depending on the discriminator it's associated with, but will always contain the fields (username
, email
city
, accountType
) present in the "base" model, Profile
.
Before I send my PUT request, an example of a document in Profile
looks like this:
{ jobTitle: '',
lastPosition: '',
email: '',
city: '',
accountType: 'helper',
_id: 5c77883d8db04c921db5f635,
username: 'here2help',
__v: 0 }
This looks good to me, and suggests that the model is being created as I want (with base fields from Profile
, and those associated with the Helper
model - see below for models).
My POST route then looks like this:
router.put("/profile/:id", middleware.checkProfileOwnership, function(req, res){
console.log(req.body.profile);
Profile.findOneAndUpdate(req.params.id, req.body.profile, function(err, updatedProfile){
if(err){
console.log(err.message);
res.redirect("/profile");
} else {
console.log(updatedProfile);
res.redirect("/profile/" + req.params.id);
}
});
});
The information I receive from the form (console.log(req.body.profile)
) is what I expect to see:
{ accountType: 'helper',
username: 'here2help',
email: 'helpingU@me.com',
city: 'New York',
jobTitle: 'CEO',
lastPosition: 'sales rep'}
And after the Profile.findOneAndUpdate()
:
console.log(updatedProfile)
{ jobTitle: '',
lastPosition: '',
email: 'helpingu@me.com',
city: 'New York',
accountType: 'helper',
_id: 5c77883d8db04c921db5f635,
username: 'here2help',
__v: 0 }
So, the fields that are defined in my 'Base' model (ie those defined in ProfileSchema
- see below) are being updated, but those that are in my discriminators are not - (see below and my related question).
The updated information is clearly present in req
, but is not propagated to the Profile
model - How can this be?
I've also tried using findByIdAndUpdate
but I get the same result.
Here is models/profile.js
where I define the schemas:
var options = { discriminatorKey: 'accountType' };
var profileSchema = new mongoose.Schema({
username: String,
email: { type: String, default: "" },
city: { type: String, default: "" }
}, options);
profileSchema.plugin(passportLocalMongoose);
var Profile = mongoose.model('Profile', profileSchema);
var Helper = Profile.discriminator('helper', new mongoose.Schema({
jobTitle: { type: String, default: "" },
lastPosition: { type: String, default: "" },
}));
var Finder = Profile.discriminator('finder', new mongoose.Schema({
position: { type: String, default: "" },
skills: Array
}));
module.exports = Profile;
This is my first attempt at using discriminators in mongoose, so it's more than possible that I am setting them up incorrectly, and that this is the root of the problem.
Please let me know if this is unclear, or I need to add more information.
from Mongoose model not updated in findOneAndUpdate
No comments:
Post a Comment