Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

javascript - Performing multiple MongoDB queries in one function

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...