Javascript - 错误信息:db.collection is not a function && 如何检查write batch是否成功上传数据到firestore

Javascript - Error Message : db.collection is not a function && How to check whether the write batch whether successfully upload the data to firestore

        // Get a new write batch
        const batch = writeBatch(db);

        var TransactionRecordRef = db.collection("TopUpRecord").doc();
        batch.set(TransactionRecordRef, {
            Amount: FinalTopUpAmount,
            DateTime: serverTimestamp(),
            StudentID: StudentID,
        });
               

        var TopUptoUserRef = db.collection("user").doc(UserID);
        batch.update(TopUptoUserRef, { "studentAmount": UserTotalAmount });

        batch.commit();

您好,以上是我的代码,需要使用write batch在TopUpRecord上创建一个新文档并更新用户的金额。现在我有两个问题要问一个是我怎么知道批处理是否成功更新记录到firestorm并且我还发现我上面的代码有一个错误“db.the collection is not a function”是任何人为什么会出现这个问题?

您将 v8-and-before 命名空间语法 (db.collection("TopUpRecord").doc()) 与 v9-and-up 模块化语法 (writeBatch(db)) 混合使用,这是不可能的.

在模块化语法中,该行将是:

const TransactionRecordRef = doc(collection(db, "TopUpRecord"));

另请参阅 Firebase documentation, and the upgrade guide 中的 v9 示例。