【问题标题】:making the data wait for the declaration of variable finish on Javascript?让数据等待 Javascript 上变量完成的声明?
【发布时间】:2020-01-11 16:39:16
【问题描述】:

我有以下代码。我想要做的是在执行调度之前等待值完成。 发生的事情是它调度并且只能获取第一个值而不是以下 foreach。

提前致谢

syncPO() {
    this.wait = true
    this.$store.dispatch('account/checkToken', this.optionValues.syncValue).then((res) => {
      this.wait = 'stepTwo'
      this.max = res.length
      res.forEach(result => {
            this.POValues.po_number = result.DocNumber
            this.POValues.vendor_id = result.VendorRef
            this.POValues.order_date=result.MetaData.CreateTime
            this.POValues.amount=result.TotalAmt
            this.POValues.currency=result.CurrencyRef
            this.POValues.qb_poId=result.Id
            this.POValues.status=result.POStatus
            this.POValues.line = result.Line
              this.$store.dispatch('purchaseOrder/createPO',this.POValues).then(res=>{
                  // console.log(this.POValues.line)
                this.value += 1;
              }).then((res)=>{
                this.$store.dispatch('purchaseOrder/create-lists')
              })
        })
      })

  },

【问题讨论】:

  • 尝试使用 Promise。 link

标签: javascript json vue.js


【解决方案1】:

这里有几件事。

你需要将函数声明为 async like

async syncPO(){  ...

等待调度,而不是等待。语法类似于

await this.$store.dispatch(
            'account/checkToken', this.optionValues.syncValue
        ).then((res) => {  ...

为了让分派的动作返回您正在等待的承诺,动作必须是 var,而不是 const

export var actions = { ...

如果你使用 axios,你的数据应该在res.data,而不是res

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多