Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

231
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda