如何使用RxFire检查文档是否已在Firestore中创建



在我的React应用程序中,我想在更新我的UI之前检查后台firebase函数是否在Firestore中创建了文档。因此,我使用RxFire的docData函数来订阅特定的"即将从背景函数创建"中的更改。道格:

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

问题是userData。Uid总是返回&;david&;不管在Firestore中创建的是什么文档。我做错了什么?它似乎是从我在引用中设置的路径返回uid,而不是在firestore中创建(或应该)的实际文档路径。当我更改引用以查找"users/foo"然后用户数据。Uid仍然返回"foo"而不是未定义或通过错误。

在这种情况下,我可以寻找userData.name。当我得到"undefined"如果该文档尚未存储在Firestore或"david"中;如果有,但我希望上面的例子以类似的方式工作。

要确认文档已创建,您应该侦听其他字段的更改,例如名称,正如您在问题中已经提到的那样。

此外,相同的创建/写操作将在中间调用更新。

最新更新