angular - Firebase在递增后检索值
问题描述
我正在通过使用来增加一个值
const increment = firestore.FieldValue.increment(1);
this.fb.afs.collection(colId).doc(docId).set({
invoiceCount: increment
}, {
merge: true
})
如何在继续使用此值后立即检索增加的值?
在同时访问情况下,以下内容是否充分或关键:
this.fb.afs.collection(colId).doc(docId).set({
count: increment
}, {
merge: true
})
.finally(() => {
this.fb.afs.collection(colId).doc(docId).valueChanges().pipe(first()).subscribe(res => {
//proceed with res.count
})
})
有什么更好的方法?
解决方案
您可以get
在更新后调用 firestore 文档,它会获取您刚刚更新的文档:
const docRef: AngularFirestoreDocument<any> = this.fb.afs.collection(colId).doc(docId).set({
count: increment
}, {
merge: true
})
docRef.get().pipe(
map(doc => doc.data())
)
.subscribe(...)
推荐阅读
- php - 将类别添加到 Elementor
- django - 我们如何在 django 中使用 on_update CASCADE?
- javascript - 查询 MongoDB,用正则表达式替换一个特定字段
- date - D3 在 xAxis 中显示相同的时间段
- sql - SQL Server 按不同列分组并创建新变量
- php - 如何使用 Laravel 填充选择?
- python - 如何将 .h5 文件转换为 .pb 文件?
- iis - 对预检请求的响应未通过访问控制检查:在标头中使用身份验证时,它没有 HTTP ok 状态
- powershell - Powershell 键/值对匹配问题
- r - 可以使用 lm() 将其他变量保持在其平均值,并将回归线绘制在双变量散点图上吗?