【发布时间】:2018-11-06 20:08:01
【问题描述】:
所以我试图在处理 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);
}
【问题讨论】:
-
我怀疑是因为您在异步操作完成之前尝试使用
data。 -
@amy 那么如何设置
runningBalance属性? -
您不必同步返回,而是使用承诺或回调,因为您正在添加尚未设置 runingBalance 的数据...
-
@V.Sambor 那我该怎么做呢?
-
我会写一个可能的例子来回答
标签: javascript google-cloud-firestore