Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
How can I save user's followers in firebase? React Native

I have two collections in my database one called Buyers and the other called Sellers. Whenever the buyer follows the seller I save the seller's Uid in a subcollection called userFollowings in the Buyers collection.

How can I save the followers Uids too? Save the buyer uid in a subcollection called userFollowers in the Sellers collection every time a buyer follows a seller?

here's my code:


const onFollow = () => {
  firebase.firestore()
      .collection("Buyers")
      .doc(firebase.auth().currentUser.uid)
      .collection("userFollowings")
      .doc(props.route.params.uid)
      .set({})


}
const onUnfollow = () => {
  firebase.firestore()
      .collection("Buyers")
      .doc(firebase.auth().currentUser.uid)
      .collection("userFollowings")
      .doc(props.route.params.uid)
      .delete()
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use a batched write, as follows, in such a way the two writes complete atomically.

  const db = firebase.firestore();

  const onFollow = () => {
    const buyerID = firebase.auth().currentUser.uid;
    const followerID = props.route.params.uid;

    const buyerFollowerRef = db
      .collection('Buyers')
      .doc(buyerID)
      .collection('userFollowings')
      .doc(followerID);

    const sellerFollowerRef = db
      .collection('Sellers')
      .doc(followerID)
      .collection('userFollowers')
      .doc(buyerID);

    const batch = db.batch();
    batch.set(buyerFollowerRef, { foo: 'bar' });
    batch.set(sellerFollowerRef, { foo: 'bar' });
    batch.commit();
  };

For the onUnfollow function, use batch.delete().


You could also use a Cloud Function triggered when the doc in the userFollowings subcollection is created.

This would be the way to go if the end-user does not have the permission to write to the userFollowers subcollection, and therefore cannot execute the above batched write.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!