簡體   English   中英

mongodb 更新文檔后,返回舊值

[英]mongodb after updating document, returns old values

router.delete('/deletepost', (req, res) => {
    // console.log(req.query.postid)
    if (req.query.category === 'forsale') {
        ForSalePosts.findById(req.query.postid)
            // .then(post => console.log(post))
            .deleteOne()
            .catch(err => console.log(err))

        AllPosts.updateOne({ user: req.query.userid },
            { $pull: { posts: { postid: req.query.postid } } })
            .catch(err => console.log(err))
            AllPosts.aggregate(
                [

                    { $match: { user: ObjectId(req.query.userid) } },
                    { $unwind: '$posts' },
                    { $sort: { 'posts.date': -1 } }

                ]
            )
                .then(posts => {
                    // console.log(posts)
                    res.json(posts)
                })
                .catch(err => res.status(404).json({ nopostfound: 'There is no posts' }))

    }
})

這是我的路線。 我正在嘗試刪除文檔中的一個項目。 該項目正在被刪除,但它返回舊值。 例如 :

Allposts 有一個帶有 posts:[postid:{type:String}, ...] 的數組> console.log(posts)) 在第一次調用時返回舊值,不更新組件。

編輯:剛剛意識到有時它會返回正確的值,但有時它也會返回舊值。 有誰知道為什么以及我能做些什么來解決它?

const mongoose = require('mongoose')
const Schema = mongoose.Schema;

const AllPostsSchema = new Schema({
    user: {
        type: Schema.Types.ObjectId,
        ref: 'users'
    },
    posts: [{
        postid: {
            type: String
        },
        title: {
            type: String
        },
        category: {
            type: String
        },
        subcategory: {
            type: String
        }, category: {
            type: String
        },
        description: {
            type: String
        },
        name: {
            type: String
        },
        price: {
            type: Number
        },
        email: {
            type: String
        },
        phonenumber: {
            type: Number
        },
        language: {
            type: String
        },
        make: {
            type: String
        },
        model: {
            type: Number
        },
        odometer: {
            type: Number
        },
        condition: {
            type: String
        },
        state: {
            type: String
        },
        town: {
            type: String
        },
        city: {
            type: String
        },
        links: [{ type: String }],

        date: {
            type: Date,
            default: Date.now
        }
    }]

})


module.exports = AllPosts = mongoose.model('allposts', AllPostsSchema)

反應函數調用:

  deletePost = (category, postid) => {
        const postinfo = {
            category: category.toLowerCase(),
            postid: postid,
            userid: this.props.auth.user.id
        }

        this.props.deletePost(postinfo)
    }

您需要添加options參數來delete如:

AllPosts.updateOne({ user: req.query.userid }, 
    { 
     $pull: { posts: { postid: req.query.postid } } 
    }, 
    { new: true }
);

這將在執行操作后返回新對象。 希望這對你有用。

所有 mongo 查詢都返回部分承諾。 您必須使用.then來解決每個承諾。

在這里,您將在不使用.thenasync-await情況下連續運行所有查詢。 因此,每當您在AllPosts之后從AllPosts $pull時,您都會立即調用AllPosts聚合查詢,該查詢有時會執行,有時則不會。

所以為了讓它一一運行,你必須使用.thenasync-await

router.delete("/deletepost", (req, res) => {
  if (req.query.category === "forsale") {
    ForSalePosts.findById(req.query.postid)
      .deleteOne()
      .then(() => {
        AllPosts.updateOne(
          { "user": req.query.userid },
          { "$pull": { "posts": { "postid": req.query.postid } } }
        )
          .then(() => {
            AllPosts.aggregate([
              { "$match": { "user": ObjectId(req.query.userid) } },
              { "$unwind": "$posts" },
              { "$sort": { "posts.date": -1 } }
            ]).then(posts => {
              // console.log(posts)
              res.json(posts);
            });
          })
          .catch(err =>
            res.status(404).json({ "nopostfound": "There is no posts" })
          );
      });
  }
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM