// Get a new write batch
const batch = writeBatch(db);
var TransactionRecordRef = doc(collection(db, "TopUpRecord"));
batch.set(TransactionRecordRef, {
Amount: FinalTopUpAmount,
DateTime: serverTimestamp(),
StudentID: StudentID,
});
//var TopUptoUserRef = doc(collection(db, "user"));
const UQuery = query(collection(db, "user"), where("studentID", "==", StudentID));
batch.update(UQuery, { "studentAmount": increment(FinalTopUpAmount) });
batch.commit();
@firebase/firestore:Firestore(9.8.1):AsyncQueue 無法持久寫入:TypeError:無法讀取未定義的屬性(讀取“路徑”)

我想使用 writebatch 更新根據 studentID 搜索的 studentAmount 欄位,但它不斷彈出錯誤訊息 @firebase/firestore: Firestore (9.8.1): AsyncQueue Failed to persist write: TypeError: Cannot read properties未定義(閱讀“路徑”)。有誰知道為什么會出現這種問題,有什么解決方案嗎?
uj5u.com熱心網友回復:
在查看上面的代碼后,您只查詢了缺少執行查詢的檔案。您需要呼叫該getDocs方法來檢索檔案。請參閱下面的示例代碼:
import { getDocs } from "firebase/firestore";
// Get a new write batch
const batch = writeBatch(db);
var TransactionRecordRef = doc(collection(db, "TopUpRecord"));
batch.set(TransactionRecordRef, {
Amount: FinalTopUpAmount,
DateTime: serverTimestamp(),
StudentID: StudentID,
});
//var TopUptoUserRef = doc(collection(db, "user"));
const UQuery = query(collection(db, "user"), where("studentID", "==", StudentID));
// Get the documents from `Uquery`
const querySnapshot = await getDocs(UQuery);
// Loop all the retrieved documents
querySnapshot.forEach((doc) => {
// Get the document reference by using `doc.ref`
batch.update(doc.ref, { "studentAmount": increment(FinalTopUpAmount) });
});
batch.commit();
對上述代碼添加了一些注釋。有關更多資訊,您可以查看此檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/481043.html
標籤:javascript 火力基地 谷歌云火库
