I am trying to remove a card
from the user's current project.
Each card follows the CardSchema
.
I'm building a kanban board - this might be useful to know.
However, I've been unable to delete a card.
The structure of my database for this is as follows:
"cards": [
{
"pending": {
"cardIDHere": {
// stuff here
}
},
"inprogress": {},
"completed": {}
}
]
// or
project
| - cards [Array]
| - 0 [Object]
| - pending [Object]
| - cardIDHere [Object]
| - inprogress [Object]
| - completed [Object]
In other words, to get to a card you need:
cards.0.<panel-name>.<card-id>
.
panel-name
is the parent of the card: pending
, etc.
My code:
exports.updateCardPlace = async(req, res, next) => {
const parent = await Card.findOne({"id":req.body.id});
const path = `cards.0.${parent.panel}`;
try{
await Card.findOneAndDelete({"id": req.body.id});
res.status(204).json({
status: "Success"
})
} catch(err){
res.status(300).json({
status: "Error",
message: err.message
})
}
}
Please note, req.body.id
is the correct value, and parent.panel
is valid.
However, I test this once - POST request comes back with code 204
. Ok, great. However, my error alert is shown.
So, I reload - maybe it's been deleted, as it should have been.
Nope, still there. Let's try deleting the card again.
Ah - an error this time:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'panel' of null
at exports.updateCardPlace
I have absolutely no idea what this is about - could anyone help me?
Card Schema:
const cardSchema = new mongoose.Schema({
summary: String,
description: String,
tags: Array,
urgency: String,
id: String,
panel: String
});
Project Schema:
const projectSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Project name is required']
},
type: String,
photo: String,
members: [String],
code: String,
cards: [{
"pending": {
type: mongoose.Schema.Types.Object,
ref: 'Card'
},
"inprogress": {
type: mongoose.Schema.Types.Object,
ref: 'Card'
},
"issues": {
type: mongoose.Schema.Types.Object,
ref: 'Card'
},
"completed": {
type: mongoose.Schema.Types.Object,
ref: 'Card'
}
}],
tags: Object
});