0

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}]
            }
2
  • You will be creating this entire tag object and pushing it isn't, is the question is how to get the output as u mentioned like the tag variable, didn't find the new values to be added. Can you add that method also Commented Jul 21, 2020 at 17:26
  • I want the same like the tag object should create in Firestore, till now I am adding with that set, I want array update query in the FIrestore to append that users in an array Commented Jul 21, 2020 at 17:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.