0

I am working with Firebase Firestore using Nuxt js and I want to retrieve data from my database. I am using firebase with NuxtFirebase, the official plugin for firebase. Though I get a list of objects instead of an array, how do I convert it into an array?

//post.js





const actions = {

// The Post list API 


async postList({commit}){

    try{
      
      
      const req = await this.$fire.firestore.collection('Post').get()
  
      req.forEach((doc) => {
          // doc.data() is never undefined for query doc snapshots

          const posts = doc.data()
          

          console.log(posts)

         commit("SET_POST", posts)
          
        });
        
       
    
      
      }
      catch(e){
      
      console.log(e)
      }
      
      },
  





};


export default {

state,
getters,
mutations,
actions,
}


1 Answer 1

1

The following should do the trick:

const querySnapshot = await this.$fire.firestore.collection('Post').get()
const arrayOfDocs = querySnapshot.docs.map((doc) => doc.data());

The docs property of a QuerySnapshot returns an array of all the documents in the QuerySnapshot.

Sign up to request clarification or add additional context in comments.

6 Comments

Glad I could help you @UdemezueJohn! You may accept and upvote the answer in such a way other SO users with the same problem see that this answer solved the problem.
Thanks , I will do that, also I need a little bit of help, I want to get some data out of the Query such as doc.id and many more, I tried formating the code this way. ``` const arrayOfDocs = querySnapshot.docs.map((doc) => { 'id':doc.id, 'content':doc.content }); ```
You should use doc.data(). content is not a property of a DocumentSnapshot.
I need to Query the ID and some specific details in the Array
I understand that content is a field of the document. If this assumption is true then do as follows: const arrayOfDocs = querySnapshot.docs.map((doc) => { 'id':doc.id, 'content':doc.get('content') }); (see the doc for the get() method). Also, do not forget to accept the answer if it solved your question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.