【问题标题】:Await doesn't await with firebase batches functionsAwait 不使用 firebase 批处理功能等待
【发布时间】:2021-10-17 15:02:07
【问题描述】:

嗯,有一个简单的函数可以将加载程序的值设置为 true,然后在函数完成后将其设置为 false。但是有一个问题,我在 async/await async/await 函数中的等待没有等待具有 firebase batch writes 的代码。我的代码:

const updateNegotiation = async title => {
  negotiationLoader.value = true
  await store.dispatch('actionUpdateNegotiation', {
  negotiation: listFromDialog.value, 
  title: title, 
  loader: negotiationLoader.value
  })
  negotiationLoader.value = false // the code doesnt await and put th loader's value to false
}

这是我的 batch writesfirebase 函数

//vuex

async actionUpdateNegotiation({commit}, deal){
        const batch = db.batch()
        const reference = db.collection('negotiations')
        .doc(moduleUser.state.user.email)
        .collection('deals')

        const setRef = reference.doc(deal.title)
        .collection('clients')
        .doc(deal.negotiation.id)

        batch.set(setRef, deal.negotiation)
        batch.update(setRef, {
          ...deal.negotiation,
          status: deal.title
        })
        const deleteRef = reference.doc(deal.negotiation.status)
        .collection('clients')
        .doc(deal.negotiation.id)

        batch.delete(deleteRef)
        
         try{
          batch.commit()
          return {
            res: false
          }
        } catch (error) {
          console.log(error);
          return{
              error,
              res: true
          }
        }
      }

【问题讨论】:

    标签: javascript firebase vue.js google-cloud-firestore async-await


    【解决方案1】:

    await 关键字仅在您调用的代码返回承诺时才起作用。您似乎所做的只是将actionUpdateNegotiation 标记为async,这还不够。

    据我所知,actionUpdateNegotiation 中唯一的异步操作是对batch.commit() 的调用,因此您可以让函数返回一个承诺:

    return batch.commit()
    

    或者你也可以使用await:

    await batch.commit()
    

    【讨论】:

    • 正如弗兰克所说,您没有从batch.commit() 回复您的承诺。目前actionUpdateNegotiation 立即返回,作为已解决的承诺(无异步)
    • 正如弗兰克所说,您没有从batch.commit() 回复您的承诺。目前actionUpdateNegotiation 立即返回,作为一个已解决的承诺(什么都没有异步)。一旦你把async 放在一个返回promise 的函数上,你的函数返回{ res: false},它会自动解析为{res: false};
    【解决方案2】:

    你必须将你的 actionUpdateNegotiation 包装在一个承诺中。

    async actionUpdateNegotiation({commit}, deal){
            return new Promise(function (resolve, reject) {
                const batch = db.batch()
            const reference = db.collection('negotiations')
            .doc(moduleUser.state.user.email)
            .collection('deals')
    
            const setRef = reference.doc(deal.title)
            .collection('clients')
            .doc(deal.negotiation.id)
    
            batch.set(setRef, deal.negotiation)
            batch.update(setRef, {
              ...deal.negotiation,
              status: deal.title
            })
            const deleteRef = reference.doc(deal.negotiation.status)
            .collection('clients')
            .doc(deal.negotiation.id)
    
            batch.delete(deleteRef)
            
             try{
              batch.commit()
              resolve(false);
            } catch (error) {
              console.log(error);
              reject({
                  error,
                  res: true
              });
            }
            });
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      相关资源
      最近更新 更多