I'm creating a kanban board (e.g. Trello), and am having problems with moving the cards across the panels.
When a card is dropped on a panel, it should:
- Delete its previous card (i.e. pending --> completed, we would delete the card from pending)
- Add the card to the new panel
- Change the card's
panel
field to the new one
The structure of a project:
Project
| - cards [Array]
| - 0 [Object]
| - pending [Object]
| - card here [Object]
| - inprogress [Object]
| - issues [Object]
| - completed [Object]
My code:
exports.updateCardPlace = catchAsyncErr(async(req, res, next) => {
const card = await Card.findOne({"id":req.body.id});
console.log(card)
try {
const q = `cards.0.${card.panel}.${req.body.id}`;
const q1 = `cards.0.${req.body.newSection}.${req.body.id}`;
const q2 = `cards.0.${req.body.newSection}.${req.body.id}.panel`;
await Project.updateOne(
{[q]: {$exists: true}},
{$unset: {[q]: ""}});
await Project.updateOne(
{[q]: {$exists: false}},
{$set: {[q1]: card}});
await Project.updateOne(
{[q1]: {$exists: true}},
{$set: {[q2]: req.body.newSection}});
res.status(204).json({
status: "Success"
})
} catch(err){
res.status(300).json({
status: "Error",
message: err.message
})
console.log(err.message)
}
})
Here's my problem.
(Cards have a default panel
value of pending
, as this is where they go when created)
- Cards moved from
pending
work fine - they delete the pending version of themselves, etc.
- Cards moved from anywhere else do not delete themselves.
- With these cards,
console.log(card.panel)
prints pending
. However, in the database (MongoDB Compass), it shows the panel
has changed as it should.
Would anyone be able to help me?
Thanks.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…