Home > Back-end >  Javascript - FirebaseClould Issues: Cannot read properties of undefined (reading 'path')
Javascript - FirebaseClould Issues: Cannot read properties of undefined (reading 'path')

Time:05-25

        // 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 Failed to persist write: TypeError: Cannot read properties of undefined (reading 'path')

enter image description here

I would like to use writebatch to update the studentAmount Field which searches based on the studentID but it keeps on popping up an error message @firebase/firestore: Firestore (9.8.1): AsyncQueue Failed to persist write: TypeError: Cannot read properties of undefined (reading 'path'). Does anyone know why this kind of issue and is there any solution?

CodePudding user response:

Upon reviewing your code above, you only queried the document(s) which lacks execution of the query. You need to call the getDocs method to retrieve the documents. See sample code below:

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();

Added some comments on the above code. For more information, you may check this documentation.

  • Related