Alexy Riche
Alexy Riche

Reputation: 27

How to access to the data of an object?

I have a function which reads the data from the Firebase. I can read the objects but not the properties.

I try to parse the content but I have an error.

This is the function:

 this.afAuth.authState.subscribe(data => {
            this.itemsList = this.afDatabase.list(`oders/${data.uid}`).valueChanges();
            
            console.log(this.itemsList)
            this.itemsList.forEach(data =>{
              console.log(data) // this work
              console.log(data.name) // this doesn't work (undifined in console)
            })
          })

My JSON OBJECT

(14) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {hour: 1567395936608, id: 0, name: "Salami", price: "8"}
1: {hour: 1567395936608, id: 9, name: "Suplémment Fromage", price: "8"}
2: {hour: 1567395936608, id: 4, name: "bolo", price: "8"}
3: {hour: 1567395936608, id: 4, name: "bolo", price: "8"}
4: {hour: 1567395985542, id: 4, name: "bolo", price: "8"}
5: {hour: 1567395985542, id: 0, name: "Salami", price: "8"}
6: {hour: 1567395985542, id: 9, name: "Suplémment Fromage", price: "8"}
7: {hour: 1567397849623, id: 6, name: "Dems", price: "15"}
8: {hour: 1567397849623, id: 0, name: "Salami", price: "8"}
9: {hour: 1567397849623, id: 9, name: "Suplémment Fromage", price: "8"}
10: {hour: 1567398309241, id: 0, name: "Salami", price: "8"}
11: {hour: 1567398309241, id: 9, name: "Suplémment Fromage", price: "8"}
12: {hour: 1567402336588, id: 0, name: "Salami", price: "8"}
13: {hour: 1567402336588, id: 9, name: "Suplémment Fromage", price: "8"}

Thanks!

Upvotes: 0

Views: 56

Answers (2)

ranjeet8082
ranjeet8082

Reputation: 2359

Using spread operator.

[...this.itemsList].forEach(item) {
 console.log(item.hour);
}

Upvotes: 0

Adrita Sharma
Adrita Sharma

Reputation: 22213

Since data is an array itself, you need to run another loop

Try like this:

this.itemsList.forEach(data =>{
   this.data.forEach(e=>{
       console.log(e.name) 
   })
})

or you can do this:

this.itemsList[0].forEach(data =>{
    console.log(data.name) 
})

Upvotes: 1

Related Questions