So, I have a Category model with three discriminators on it. One of them, "Allowance" has a new Schema for an extra field. However, when a new Category is saved to the DB, that field will not save, it is being thrown out by Mongoose.
const CategorySchema = new mongoose.Schema({
name: {
type: String,
required: [true, "MUST HAVE A NAME"],
validate: {
validator: isSanitary,
message: "NAME CONTAINS ILLEGAL CHARACTERS"
}
},
amount: {
type: Number,
required: [true, "MUST CONTAIN AN AMOUNT"]
},
removed: {
type: Boolean,
default: false
}
}, {discriminatorKey: "kind"});
const Category = mongoose.model("Category", CategorySchema);
Category.discriminator("Income", new mongoose.Schema());
Category.discriminator("Bill", new mongoose.Schema());
Category.discriminator("Allowance",
new mongoose.Schema({
isPercent: {
type: Boolean,
required: true
}
})
);
module.exports = {
Income: Income,
Bill: Bill,
Allowance: Allowance,
CategorySchema: CategorySchema
};
Requiring the model:
const {Income, Bill, Allowance} = require("../models/category.js");
Code for creating a new Category:
/*
POST: create a new Category
req.body = {
account: String (Account id)
name: String
amount: Number
kind: String (Income, Bill, Allowance)
isPercent: Boolean
}
response: Category
*/
createCategory: function(req, res){
let category = {
name: req.body.name,
amount: req.body.amount,
removed: false
};
switch(req.body.kind){
case "Income":
category = new Income(category);
break;
case "Bill":
category = new Bill(category);
break;
case "Allowance":
category = new Allowance(category);
category.isPercent = req.body.isPercent;
break;
}
res.locals.user.accounts.id(req.body.account).categories.push(category);
res.locals.user.save()
.then((user)=>{
return res.json(category);
})
.catch((err)=>{
console.error(err);
return res.json("ERROR: unable to create new category");
});
},
When a new "Allowance" is saved, it saves fine, except that isPercent is not saved to the DB. This was working previously but then stopped when I made a tiny change. The only change is that I started exporting the Schema as well as the model. I have no idea how to even debug this issue.
Thanks for any help.