lukas34tfd
lukas34tfd

Reputation: 135

push firebase data inside an array + js + vuejs

I have in firebase firestore a Collection named users created with docs of unique id.

Now I would like to push them in an Array.

(In the usersCollection there are 3 users stored with the currentUser.uid)

Example:

fb.usersCollection.where("state", "==", 'online').get().then(querySnapshot => {
      querySnapshot.forEach((doc) => {
         const userName = doc.data().name

  this.markerMy = { name: userName }
})

// push userName inside randomArray
const randomArray = []
randomArray.push(this.markerMy)

I only get it so that I can push one user inside the Array, but not more.

Upvotes: 1

Views: 797

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

You should declare randomArray before fb.usersCollection and call the push operation inside the callback as follows :

const randomArray = []
fb.usersCollection.where("state", "==", 'online').get().then(querySnapshot => {
      querySnapshot.forEach((doc) => {
        const userName = doc.data().name

        this.markerMy = {
          name: userName
        }

        randomArray.push(this.markerMy)
      })
   });

Upvotes: 3

Related Questions