首页 > 解决方案 > 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
  })
})

有什么更好的方法?

标签: angularfirebasegoogle-cloud-firestore

解决方案


您可以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(...)

推荐阅读