Bill Pope
Bill Pope

Reputation: 591

Firebase query - not able to push document data into array

I'm noticing the last document gets pushed twice into the array but the first document doesn't get pushed in at all. What gives? So it turns out it's not a problem with the date, something is up with the query, I think

 let obj = {}
 let items = []
db.collection('customer').doc(id).collection('appointments').get().then((snapshot) => {
          snapshot.forEach((item) => {
            let data = item.data()
            console.log(item.id)
            obj.start = data.start.toDate()
            obj.end = data.end.toDate()
            obj.title = data.title
            obj.desc = data.desc
            //console.log(date)
            items.push(obj)  
            //console.log(items.length)   
          })

Upvotes: 0

Views: 97

Answers (1)

Abdullah Z Khan
Abdullah Z Khan

Reputation: 1286

From what I can surmise, you're using an object reference to push into the array. Whenever that object gets changed, all the references of that object in that array will automatically change. So you might see regardless of the number of items you push, all of them will be the last document data.

you can try moving

let obj = {}

to

>let obj = {}
let data = item.data()

here, inside the loop.

Let me know if this works!!

Upvotes: 3

Related Questions