1
votes

I am trying to get a post title from firestore but somehow I could not figure out how that could be done using async await.

async getVideo(id) {
  var self = this;
  const ref = this.$fire.firestore
    .collection("posts")
    .where("ytid", "==", id)
    .orderBy("createdAt", "desc");
  try {
    let post = await ref.get();
    console.log(post.data());
  } catch (e) {
    console.log(e);
  }
}

I tried to console log post.data() but it says post.data() is not a function. Any help would be appreciated.

2
What's wrong with what you have now? Please edit the question to include you debugging information.Doug Stevenson

2 Answers

1
votes

You are retrieving multiple documents, so post will be a snapshot of documents which does not have a data() method.

You'll need to iterate through the snapshot to access the individual documents.

See https://firebase.google.com/docs/firestore/query-data/get-data#get_multiple_documents_from_a_collection for a quick guide or https://googleapis.dev/nodejs/firestore/latest/QuerySnapshot.html for a full reference of the QuerySnapshot type.

1
votes

When you call ref.get(), you will get a QuerySnapshot object. This object contains zero or more DocumentSnapshot objects that contain the data from the query results. QuerySnapshot does not have a method called data(). You will have to iterate the documents using the provided API to get the DocumentSnapshots:

const qsnapshot = await ref.get();
qsnapshot.forEach(doc => {
    const data = doc.data();
    console.log(data);
})