// 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();
Hi, the above is my code that needs to use write batch to create a new document on TopUpRecord and update the user's amount. Now I have two questions that I need to ask one is how can I know whether the batch successfully updates the record to the firestorm and I also found that my above code has an error which "db. the collection is not a function" Is anyone why this issue happened?
You're mixing the v8-and-before namespaced syntax (db.collection("TopUpRecord").doc()) with the v9-and-up modular syntax (writeBatch(db)), which isn't possible.
In the modular syntax, that line would be:
const TransactionRecordRef = doc(collection(db, "TopUpRecord"));
Also see the v9 examples in the Firebase documentation, and the upgrade guide.