在我的React应用程序中,我想在更新UI之前检查后台firebase函数是否在Firestore中创建了文档。因此,我使用来自RxFire的docData函数来订阅特定"about-to-be-created-from-a-background-function“文档中的更改:
const davidDocRef = db.doc('users/david');
// meantime, a firebase function creates "users/david" doc in the background
// I am observing for changes to the firestore doc in my React Component:
useEffect(() => {
docData(davidDocRef,'uid').subscribe(userData => setUserId(userData.uid));
...
// I then check that orgId is not null by assuming that I will get back a doc.id after it has been created it in Firestore问题是无论在Firestore中创建什么文档,userData.uid总是返回"david“。我做错了什么?它似乎是从我在引用中设置的路径返回uid,而不是已经(或应该)在firestore中创建的实际文档路径。当我更改引用以查找"users/foo“时,userData.uid仍然返回"foo”,而不是未定义的或通过错误返回。
在这种情况下,我可以改为查找userData.name。实际上,如果文档没有存储在Firestore中,我就会得到"undefined“,如果文档已经存储在Firestore中,我就会得到"David”,但我希望上面的例子也能以类似的方式工作。
发布于 2021-02-16 22:56:37
"uid“字段的值不会改变,因为它是对文档的引用。
要确认文档已创建,您应该侦听其他字段中的更改,如名称,正如您在问题中提到的那样。
此外,相同的创建/写入操作will invoke intermediately the updates。
https://stackoverflow.com/questions/66152331
复制相似问题