我正在使用Firebase Stripe擴展。
將用戶添加到 Firebase 后,Stripe 擴展程式使用 Cloud Functions 在 Firestore 資料存盤區中創建users檔案。
在我的應用程式中,我有一個創建 Firebase 用戶的函式,然后嘗試更新匹配的usersFirestore 檔案。
但有時 Cloud Functions 尚未完成創建users檔案并回傳此錯誤:
No document to update: projects/APP_NAME/databases/(default)/documents/users/KHaY21FSUasdfOXFS123Kfge85VA3
為了解決這個問題,我使用了timeout在繼續之前暫停幾秒鐘的 a。這不是一個理想的解決方案。
相反,我想知道 Firebase Stripe 擴展是否users在繼續之前完成了檔案的創建。
怎么可能呢?
這是我的應用程式功能:
export const useCreateUserWithEmailAndPasswordTask = () => {
const updateDocTask = useUpdateDocTask();
return useTask(function* (
signal,
email: string,
password: string,
firstName: string,
lastName: string,
) {
// Create the user in Firebase
// The Firebase Stripe extension will now use Cloud Functions to also create a 'users' doc with related user info
const userCredential: UserCredential = yield createUserWithEmailAndPassword(
auth,
email,
password
);
// Now I wait 3 seconds to make sure the 'users' doc is created before updating it.
// Without this timeout it will occasionally throw an error because updateDocTask is called before the 'users' doc is created
// Is there a better way of doing this?
yield timeout(3000);
const updateDoc: void = yield updateDocTask.perform(
'users',
userCredential.user.uid,
{
firstName: firstName,
lastName: lastName,
createdAt: timestamp,
}
);
return Promise.all([userCredential, updateDoc]);
});
};
uj5u.com熱心網友回復:
您可以使用實時偵聽器,而不是setTimeout()如下所示:
import { doc, onSnapshot } from "firebase/firestore";
const unsub = onSnapshot(doc(db, "users", "<USER_DOC_ID>"), (doc) => {
// This function triggers whenever the document is created/updated/deleted
console.log("Data: ", doc.data());
});
請檢查是否doc.data()已經有資料,以防擴展在用戶到達此頁面之前添加了檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442305.html
標籤:javascript 火力基地 谷歌云火库 条纹支付
上一篇:在Xamarin中關閉Popup時如何回傳TabbedPage的上一頁
下一篇:添加Firestore規則“&&request.auth.uid==userId;”導致“快照偵聽器中的錯誤:FirebaseError:缺少權限或權限不足。”
