简体   繁体   中英

How to push the user object in the Array in Firebase Firestore

What I did to update that count of the likes and dislikes with Firestore?

const tagDatabase = firebase.firestore().collection("tags").doc(TAGID);
  tagDatabase.get().then((res) => {
    if (res.exists) {
      const getData = res.data();
      setTaginfo(getData);
    }    
  const updateData = {
              _id: tagInfo._id,
              updateAt: new Date(),
              likes: tagInfo.likes - 1,
              dislikes: tagInfo.dislikes + 1,
              status: true,
              user:[{userid:"675",liked:true,disliked:false}]
            };
            tagDatabase.set(updateData);

What to do now to update array by inserting users in the user array of that object in the firebase, I want like below:

 tag={    _id: "7252525",
              updateAt: 15/07/2020,
              likes: 5,
              dislikes: 3,
              status: true,
              user:[{userid:"675",liked:true,disliked:false},
                     {userid:"677",liked:false,disliked:false},
                   {userid:"698",liked:false,disliked:false}]
            }

Looks like you need array union operation. Update elements in an array

You can use array union to add new items to an array, and also to remove them. Plus point is that you don't have to read the document beforehand and perform a transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM