0

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)   
          })

1 Answer 1

3

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!!

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

3 Comments

Yup. That did it. It's because objects are passed by reference, I guess. Thanks
You're wecome! :)
Hi, if my answer solved your problem, could you mark it as accepted? Or comment otherwise (e. g. Errors)

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.