Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

228
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda