简体   繁体   中英

Update to database happens more than it should - vue.js this.$router.push and express

I am trying to update a mongo db through this code. The problem is that it updates 3 times instead of once (the current data I have has 3 dates in posts.date) I am using vue mongo express this.posts is the data and the data looks something like this

  {
  "posts": [
    {
      "date": [
        {
          "date": "180320",
          "attend": "present"
        },
        {
          "date": "180321",
          "attend": "absent"
        },
        {
          "date": "180322",
          "attend": "present"
        }
      ],
      "_id": "5ab7a6396b92178b5a9c5118",
      "netid": "cjp",
      "name": "Colton"
    },
    {
      "date": [
        {
          "date": "180320",
          "attend": "present"
        },
        {
          "date": "180321",
          "attend": "present"
        },
        {
          "date": "180322",
          "attend": "present"
        }
      ],
      "_id": "5ab7a6466b92178b5a9c5119",
      "netid": "do",
      "name": "Daniela"
    },
    {
      "date": [
        {
          "date": "180320",
          "attend": "present"
        },
        {
          "date": "180321",
          "attend": "absent"
        },
        {
          "date": "180322",
          "attend": "absent"
        }
      ],
      "_id": "5ab7a6516b92178b5a9c511a",
      "netid": "msa",
      "name": "Marcus"
    },
    {
      "date": [
        {
          "date": "180320",
          "attend": "present"
        },
        {
          "date": "180321",
          "attend": "absent"
        },
        {
          "date": "180322",
          "attend": "late"
        }
      ],
      "_id": "5ab7bf736b92178b5a9c511b",
      "netid": "test123",
      "name": "Test"

The code looks like this. target is the current student. each if statement checks for the current student in this.posts and the direction (doesn't matter which way) problem is await PostsService.updatePost is updating the db 3 times, when it should only once.

  async updatePost (target, direction) {
  let day = this.today
  for (let student in this.posts) {
    if (this.posts[student].name == target && direction == 'left') {
      let dateupdate = {date: day, attend: "present"}
      let datearr = this.posts[student].date
     //some more code in the middle
      await PostsService.updatePost({
        id: this.posts[student]._id,
        netid: this.posts[student].netid,
        name: this.posts[student].name,
        date: this.posts[student].date
      })
    }
    if (this.posts[student].name == target && direction == 'right') {
      let dateupdate = {date: day, attend: "absent"}
      let datearr = this.posts[student].date
      //some more code in the middle
      await PostsService.updatePost({
        id: this.posts[student]._id,
        netid: this.posts[student].netid,
        name: this.posts[student].name,
        date: this.posts[student].date
      })
    }
  }
    this.$router.push({ name: 'Posts' })
}

i am really lost and don't know what to do :( (can't really upload a fiddle bc it's part of a bigger project) thank you in advance!

I believe this might be a cause of the for loop not supporting async/await .
That is, the for loop runs through all iterations without "awaiting" for the asynchronous code you are running to finish.
I recommend you take a look at something like bluebirds's promise.map function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM