0

Actually am new in react and am trying to create an event app in which a user can join an event here is code for joining an event

export const JoinEvent = (id) => {
  return async dispatch => {
    let data = await firebase.firestore().collection('Events').doc(id).get()
    let tmpArray = data.data()
    let currentUser = firebase.auth().currentUser
    let newArray = tmpArray.PeopleAttending


    await firebase.firestore().collection('Events').doc(id).update({
      PeopleAttending : {...newArray, [currentUser.uid]: {displayName : currentUser.displayName}}
    })

  }
}

actually i have created an action bascailly in JoinEvent an id is passed of the particular event which is clicked.

here is my firestore structure look like this.. enter image description here

so basically i have to download the whole data and store in local array and then add new user and then finally update So here am basically download the whole data is there any way to just simply add new Object without downloading whole data?? thankyou

Himanshu Rahi
  • 255
  • 4
  • 15

1 Answers1

0

You are doing it wrong. Firestore document size limit is Maximum size for a document 1 MiB (1,048,576 bytes), so sooner or later you're going to reach that limit if you keep adding data like this. It may seems that you're not going to reach that limit, but it's very unsafe to store data that way. You can check Firestore query using an object element as parameter how to query objects in firestore documents, but I suggest you don't do it that way.

The proper way to do it, is to create a subcollection PeopleAttending on each document inside the Events collection and then use that collection to store the data.

Also you can try document set with merge or mergeFields like documented here https://googleapis.dev/nodejs/firestore/latest/DocumentReference.html#set and here https://stackoverflow.com/a/46600599/1889685.

Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
  • I Actually Created PeopleAttending inside the Events Collection I Update my question check my Database Structure, but i want to know data is there anyother way to update data without any downloading whole data – Himanshu Rahi Oct 27 '19 at 19:26
  • I still see the `PeopleAttending` as a field of `SuWja...` document. Did you try the `set` with `merge` option? – Christos Lytras Oct 27 '19 at 19:38