Javascript - FirebaseClould 问题:无法读取未定义的属性(读取 'path')

Javascript - FirebaseClould Issues: Cannot read properties of undefined (reading 'path')

        // 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:无法读取未定义的属性(读取 'path')

我想使用 writebatch 来更新基于 studentID 搜索的 studentAmount 字段,但它一直弹出错误消息@firebase/firestore: Firestore (9.8.1): AsyncQueue Failed to persist write : TypeError: 无法读取未定义的属性(读取 'path')。有谁知道为什么会出现这种问题,有什么解决办法吗?

检查上面的代码后,您只查询了没有执行查询的文档。您需要调用 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();

对上面的代码添加了一些注释。 欲了解更多信息,您可以查看此 documentation.