1

I have to documents in firestore one is user document and one is tweet document. I am trying to replicate the twitter clone. So my user has follower array. I want to display the user its tweet and its follower tweet. User collection is :

{
name:test,
email:test@gmail.com,
follower:[a@gmail.com,b@gmail.com]
}

Tweet Collection is:

{
text:Dummy tweet,
by:test@gmail.com,
img:dummyImageSource
}

I am not able to figure out. If I am logged in with test@gmail.com and I have one follower (let's say a@gmail.com). How will i need to query to fetch comment of mine(test@gmail.com) and all the follower(in this case a@gmail.com).

I have tried doing something like this :

db.collection("tweets")
            .where("email", "==", this.state.user.email)//loggedin email
        //need to add follower condition as well here so that i get combined tweet

            .orderBy("created", "asc")
            .onSnapshot(snapshot => {
                let oldArr = [];
                console.log(snapshot.docs)
                snapshot.docs.forEach(doc => {
                 console.log(doc)
                });

Shubham Verma
  • 4,918
  • 1
  • 9
  • 22

1 Answers1

1

You need to make two (or more) queries to the tweets collection.

One for your user, and one for each of the user's followers.

Also note on the query condition the collection field is "by" not "email"

Like so:

const makeTweetQueryListener = (email) => db.collection("tweets")
            .where("by", "==", email)
            .orderBy("created", "asc")
            .onSnapshot()

Promise.all([
   makeTweetQueryListener(this.state.user.email)
].concat(this.state.user.followers.map(makeTweetQueryListener))
).then(values => {
    values.forEach(query => {
        query.docs.forEach(console.log)
    });
})

EDIT - changed the code above to add a listener instead of just querying

Carlos Sá
  • 602
  • 3
  • 10