I have a collection in firebase called "community" and "users". All user records have a field "joinedCommunity" (a list of all joined communities).
I’m trying to figure a code that when a community is deleted, all user records are updated to only remove the community reference from "joinedCommunity" field list.
building this in flutterflow using custom action
onTap on a button in UI, the code is included as one of the actions before community document is deleted.
Future userRecordUpdate(DocumentReference community) async {
final instance = FirebaseFirestore.instance;
final batch = instance.batch();
var collection = instance.collection('users');
batch.update(collection, {
"joinedCommunity": FieldValue.arrayRemove([community])
});
await batch.commit();
}
2
Answers
the following code worked -
You’re using a
CollectionReference
, when what you want is aDocumentReference
. As per the documentation,WriteBatch.update
only works on aDocumentReference
.I have a few suggestions:
DocumentReference.update()
call.WriteBatch
to update the field. Also, keep in mind a batch is limited to 500 operations.