Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在VueJS方法中继续之前等待Axios请求?_Javascript_Vue.js_Async Await_Axios - Fatal编程技术网

Javascript 如何在VueJS方法中继续之前等待Axios请求?

Javascript 如何在VueJS方法中继续之前等待Axios请求?,javascript,vue.js,async-await,axios,Javascript,Vue.js,Async Await,Axios,我有以下函数,当用户单击HTML元素时触发 async upvote(news) { await this.isAuthors(news) if (this.user != null) { axios.post(serverSide.findUserByID, {userID: this.user._id}) .then((res) => { this.user.rep = res.data.user.rep if (this.votingL

我有以下函数,当用户单击HTML元素时触发

async upvote(news) {
  await this.isAuthors(news)
  if (this.user != null) {
    axios.post(serverSide.findUserByID, {userID: this.user._id})
    .then((res) => {
      this.user.rep = res.data.user.rep
      if (this.votingList[news._id].isAuthors == true) {
        alert('Authors of the same publisher is not allowed to vote this news!')
        return
      }
      else () {
      // some other Axios request here
      }
    })
  }
  else {
    this.$router.push({name: 'login'})
  }
}
但是,整个逻辑取决于在发出另一个Axios请求之前正在处理的值
this.isAuthors(news)
,因此可以将
this.votingList[news.\u id].isAuthors
设置为其默认值。下面是获取所述值的函数

async isAuthors(news) {
  if (this.user.hasPublisher == true) {
    axios.post(serverSide.getPublisher, {
      userID: this.user._id, userRole: this.user.role
    })
    .then((res) => {
        if (news.publisher == res.data.publisher._id) {
          console.log("Same publishers")
          this.votingList[news._id].isAuthors = true
        }
        else {
          this.votingList[news._id].isAuthors = false
        }
    })
  }
  else {
    this.votingList[news._id].isAuthors = false
  }
}

如何使
upvote(news)
在继续函数之前等待
isAuthors(news)
获取的值?因为到目前为止,
this.votingList[news.\u id].isAuthors
仍将保持其默认值

您必须在
isAuthors
函数中等待axios.post。