Pieter Venter
Pieter Venter

Reputation: 83

Firebase update spesific collection

I want to update a score value within a specific collection that is within a document called competitions.

I would like to update a specific collections data values, but it gives me this error:

FirebaseError: Expected type 'gc', but it was: a custom yc object

My query is probably wrong, but how can I update a specific value within a collection?

const colRef= query(collection(db, 'competitions/'+id+'/scorecard'), where("id", "==", scorecardid));
return updateDoc(colRef, {finalscore:300}, { merge: true });

Underneath you can see the db struncture from competitions to scorecards the get a scorecard spesific id and the you want to update that scorecard finalscore.DB structure

DB structur 2

Upvotes: 1

Views: 51

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50830

The updateDoc() takes a DocumentReference as first parameter but you are passing a Query. There isn't any updateDocsWhere functionality in Firestore at the moment. You'll first have to run that query and then iterate over all documents and delete them using their reference. Try refactoring the code as shown below:

const colRef= query(collection(db, 'competitions/'+id+'/scorecard'), where("id", "==", scorecardid));

const snapshot = await getDocs(colRef);

const promises = snapshot.docs.map((doc) => updateDoc(doc.ref, { finalscore:300 }));

await Promise.all(promises);

Alternatively, you can also use Batched Writes to update up to 500 documents at once.

Upvotes: 1

Related Questions