firestore query error for subcollections: rest.collection is not a function

I want to count the number of branches of all restaurants. “Branch” is a sub collection of “Restaurants”. When I try to do this query, there is an error:

rest.collection is not a function

Here’s my code. How can I fix it?

async function getBranch(){ 
  const size = 0;
  const restRef = await firebase.firestore().collection('Restaurants').get();
  restRef.forEach((rest) => {
    const branchRef = rest.collection('Branch').get();
    size = size + branchRef.size();
  })
  return size;
}

Answer

you have to provide id of restaurants to get the subcollection. So better make reference for Restaurants and get all branches

async function getBranch(){ 
  const size = 0;
  const restRef = firebase.firestore().collection('Restaurants');
  const restRes = await restRef.get();
  restRef.forEach((rest) => {
    const branchRef = await restRef.doc(rest.id).collection('Branch').get();
    size = size + branchRef.size();
  })
  return size;
}