在内部设置功能参数,然后用firestore阻止

时间:2018-11-06 20:08:01

标签: javascript google-cloud-firestore

因此,在尝试处理Firestore时,我试图在then块内设置一个对象参数,但由于某种原因它没有被设置。我的语法有问题吗?我认为使用=>可以做到这一点。

updateLedger(id: string, data: any) {
    this.afs.collection('chartofaccounts').doc(id).ref.get().then(doc => {
      if (doc.data().normalside === 'debit') {
        ///// set the runningBalance of the data object passed into the function here
        data.runningBalance = doc.data().debitAmount - doc.data().creditAmount;
      } else {
        ///// or here...
        data.runningBalance = doc.data().creditAmount - doc.data().debitAmount;
      }
    });
    return this.afs.collection('ledger').add(data);
  }

1 个答案:

答案 0 :(得分:0)

这是一种可行的方法,但是我不确定您是否确实需要回调,重要的想法是,当承诺得到解决时,只需在this.afs.collection('ledger').add(data);中添加.then这一行(异步)。我还把回调函数供您检查是否有帮助。

updateLedger(id: string, data: any, callback: function) {
  this.afs.collection('chartofaccounts').doc(id).ref.get().then(doc => {
    if (doc.data().normalside === 'debit') {
      ///// set the runningBalance of the data object passed into the function here
      data.runningBalance = doc.data().debitAmount - doc.data().creditAmount;

      // You set it here maybe you don't even need the callback
      this.afs.collection('ledger').add(data);
      callback(data);
    } else {
      ///// or here...
      data.runningBalance = doc.data().creditAmount - doc.data().debitAmount;

      // You set it here maybe you don't even need the callback
      this.afs.collection('ledger').add(data);
      callback(data);
    }
  });
}

// In the call you receive the result
updateLedger(id, data, (resultData) => {
  console.log(resultData)
})

确保如果没有调用then,则添加.catch语句,也许还有其他问题...