當用戶更改其顯示名稱時,我正在嘗試更新用戶所有帖子的名稱。
我正在嘗試上一個問題中給出的答案,但遇到了 firebase 參考錯誤。我曾嘗試使用doc.ref.pathfrom 查詢,transaction.get(docRefPath) & transaction.update(docRefPath, "data")但這沒有奏效。我有點不知道它可能是什么。我是以錯誤的方式接近這個嗎?
代碼:
// cloud function
exports.changeName = functions.region('europe-west2').https.onCall((data, context) => {
const userName = data.userName;
const uid = context.auth.uid;
return `${userName}`;
});
// client side action get all the post documents from collection with users ID
const q = query(collection(db, "Posts"), where("userID", "==", uid));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
console.log(`documentReference path = ${doc.ref.path}`);
const docRefPath = doc.ref.path;
// update users name on all their posts
const changedName = httpsCallable(functions, 'changeName');
changedName({
// our data object
userName: username
}).then((result) => {
// update document with data
try {
runTransaction(db, async (transaction) => {
const postDoc = await transaction.get(docRefPath);
if (!postDoc.exists()) {
throw "Document does not exist!"
}
transaction.update(docRefPath, { userName: result.data });
});
console.log("Transaction successfully committed!");
} catch (e) {
console.log("transtaction failed: ", e)
}
console.log(doc.id, " => ", doc.data());
}).catch((err) => {
console.error(err.message);
})
});
uj5u.com熱心網友回復:
將DocumentReferencetransaction.get()作為引數而不是路徑。嘗試通過:doc.ref
const postDoc = await transaction.get(doc.ref); // <-- not doc.ref.path
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/522633.html
標籤:Google Cloud Collective javascript火力基地谷歌云功能
