Here's the answer everyone: It can't be done.
I have a huge schema that I inherited. It has an array of objects and I am targeting an array of objects nested within that. Here is the schema
board: {
"lists": [
{ "id": 12,
"cards": [
{ "id": 123,
"cssClass": "default"
},
{ "id": 124,
"cssClass": "default"
}
]
},
{
"id": 13,
"cards":[
]
}
]
}
The cards.cssClass is what I'm trying to update. I have a statics that does a great job of getting the data for a particular card id:
BoardSchema.statics.findIndivdualCard = function(cardId, cb) {
return this.aggregate([
{ "$match": { "id": 2826249 } },
{ "$unwind": "$lists" },
{ "$match": { "lists.cards.id": cardId } },
{ "$unwind": "$lists.cards" },
{ "$match": { "lists.cards.id": cardId } },
{ "$project": { "_id": 0, "lists": 1 } }
], cb)
}
So when I have a put route come up I'm calling the static and updating the cssClass:
exports.updateCard = (req, res, next) => {
const boardId = parseInt(req.params.boardId);
const cardId = parseInt(req.params.cardId);
Board.findIndivdualCard(cardId, (err, list) => {
let card = list[0].lists.cards;
list[0].lists.cards.cssClass = req.body.cssClass;
card.save((err) => {
if (err)
return next(err);
res.json(card);
});
});
};
However I'm getting an error that says card.save is not a function. So I tried list (which is the callback) and got the same error.
I'm doing something stupid here, but not sure what. I hate the schema, but there is nothing I can do about it. Any help is appreciated.
This has gotten quite frustrating. This has been flagged as a duplicate, however there is no answer on the original duplicate.
Seriously, can this be done or not? Simple answer.