Pd.Jung
Pd.Jung

Reputation: 113

How to iterate over firebase data object to list

firebase data

logging data

 this.fb.getShoppingItems().then(result =>{
   this.exercises=result;
   console.log("this exercise : ");
   console.log(this.exercises);
 })

I should list firebase data but getting error like below

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

first pic is my firebase data structure and second one is console.log result.

in html , I should iterate over exercises came from firebase data(interval_time, name, rest_time, time)

I think I should iterate over . for each date node like 20170710/20170711 ... How can I do that?

Upvotes: 4

Views: 11186

Answers (3)

Charlie
Charlie

Reputation: 4895

I figured out this way

this.service.GetBlogs().subscribe(
  response=>{
     console.log(response);


    for(let i in response)
    {
     console.log(response[i].Title);  
    }
  }
);

response[i].Title is the main solution of problemm

Upvotes: 2

PythonProgrammi
PythonProgrammi

Reputation: 23443

I have a database like this
5bs_es1
   |
   dasdaskj
   |    |
   |   domanda1: "qualcosa..."
   | 
   |
   |
   sfjdk
       |
      domanda1: "fkfgdfh"
       |
      domanda2: "fsdjfhjsdhfj"

I use this function to get all the child values

function retrieveData(){

let ref = firebase.database().ref().child("5bs_es1");

  ref.on('value', function(snapshot) {
      let snap = snapshot.val();
      for (i in snap){
       console.log("\n" + i);
        for (n in snap[i]){
             console.log(n, snap[i][n])     
      }
  }
  });
}

Another example

It is easy to iterate this way, after the configuration code, putting data into a div called 'prosegui', for example:

<div id="prosegui"></div>

  let database = firebase.database();
  let ref = database.ref("BEP");

  ref.on("value", gotData, errData);

   function gotData(data){
    prosegui.innerHTML = "";
    x = data.val();
    for (n in x){
      prosegui.innerHTML += n + ": " + x[n] + "<br>"
    }
  }
  function errData(err){
    console.log("Error");
    console.log(err);
  }

Upvotes: 1

Ziya ERKOC
Ziya ERKOC

Reputation: 839

For JavaScript I would do

firebase.database().ref("profile/user_id").on('value', function(snap){

   snap.forEach(function(childNodes){

      //This loop iterates over children of user_id
      //childNodes.key is key of the children of userid such as (20170710)
      //childNodes.val().name;
      //childNodes.val().time;
      //childNodes.val().rest_time;
      //childNodes.val().interval_time;


  });
});

Upvotes: 10

Related Questions