0
votes

I want to retrieve all the blogs to whom user is subscribing/following like Instagram. I'm using Firebase Cloud Functions and Firestore.

exports.currentUserSubscriberPost = (req, res) => {
  const subscriberDoc = db
    .collection("subscriber")
    .where("userHandle", "==", req.user.handle);
  let subscribing = [];
  const BlogsDoc = db.collection("blogs");
  subscriberDoc
    .get()
    .then(async doc => {
      doc.forEach(data => {
        subscribing.push({ subscribingUserId: data.data().userId });
      });
      if (subscribing.length > 0) {
        let ref = subscribing.map((user) => db.collection('blogs').where('userId','==',user.subscribingUserId));

        let blogsData = await db.getAll(...ref);
        return blogsData;
      } else {
        return res.json({ msg: "you are not subscribing to anyOne" });
      }
    })
    .then(data => {
      let blogs = [];
      data.forEach(data => {
        blogs.push( data.data() );
      });
      return res.json(blogs);
    }).catch(err => console.error(err))
};


1
Its a bit hard to tell since you didn't say what line the error occured on, but I'm suspicious of the db.getAll call. It takes DocumentReferences but you passed it a set of Querys. - robsiemb

1 Answers

0
votes

You need to resolve the Query objects you are obtaining using the where method. You should iterate through the queries and get the actual DocumentReference objects that you need to call getAll()