javascript - 在里面设置函数参数然后用firestore块
问题描述
所以我试图在处理firestore时在then块内设置一个对象参数,但由于某种原因它没有被设置。我的语法有问题吗?我认为 using=>
可以让我做到这一点。
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);
}
解决方案
这是一种可能的方法,但我不确定你是否真的需要回调,重要的想法只是在 promise 解析时添加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
语句,也许还有其他一些问题......
推荐阅读
- html - 如何格式化相对于表头的行?
- excel - 计数大于 0 的单元格,如果为空则跳过单元格
- sql - 连续显示字段
- c# - 是否可以在 CreatePredictionEngine<> 中动态添加 Tsrc 和 Tdest?
- r - 如何删除R中具有零值的数据框中的行
- elixir - 如何修复 Elxir 中的“**(ArgumentError)未知注册表:Rover.Registry”
- java - 从 recyclerview 中的 editText 搜索,我使用改造从 API 获取项目
- recursion - CLisp - 在快速排序中对两个列表进行排序和组合
- python - OpenCV:相机未能正确初始化
- swift - locationManager didUpdateLocations 在挂起模式下崩溃