Arrays in Firebase(Need help to accomplish new feature)


#1

Hi. It took me quite a time to get here. I have to say for so small script :slight_smile: My intent in this post is to share what I learned and find others who would like to contribute and share this kind of “collective accomplishment”. What I’m trying to do know is read and filter the Array elements and change the button state.

From RS you can create an array field, add and remove elements and more over, inside collections or sub-collections. I see this for simple cases like keeping a list of tags or keywords and all subsequent usage possibilities, such as favoriting, liking, etc.

The best approach seems to be, if you are using Firebase as your back-end, working with “firebase.firestore.FieldValue.arrayUnion” and "Remove " I guess as of now there’s no better alternative that exists. If you know any better/faster alternate to perform the same, feel free to mention here :muscle:

Even so, we still need to check if the Array contains the email we are declaring in the data slot, and then filter the button which will switch to red heart if the email is there. btw, the button have two states.

Any contribution on how to achieve this?

regards

//onClick event

var db = firebase.firestore();
var docRef = db.collection(Userposts/${postID}/postComments).doc(commentId);
var userEmail = this.props.appActions.dataSlots[‘ds_SlotUserEmail’];

 docRef.update({
       loves: firebase.firestore.FieldValue.arrayUnion({userEmail})
       }).then(() => {
      console.log('Write succeeded!');
});

Readings: