[Vuejs]-How to get all items from subcollection Firebase Firestore Vue

0👍

✅

const getCollection = (collection, id, subcollection) => {
  const comments = ref(null);
  const error = ref(null);

  // Firestore listener

  return { error, comments };
}

The initial value of comments here is null and since Firebase operations are asynchronous, it can take a while before the data loads and hence it’ll log null. If you are using comments in v-for then that might throw an error.

It’ll be best if you set initial value to an empty array so it’ll not throw any error while the data loads:

const comments = ref([]);

Additionally, if you are fetching once, use .get() instead of onSnapshot()

Leave a comment