2

sorry for my english, i have this observable:

this.productos = af.database.list('/productos', {
    query: {
      orderByChild: 'categoria',
      equalTo: this.catnombre
    }
    });

I need extract all id from here and set in a array but i dont know how, thanks.

Edit:

I can extract the id but I use de key, now i need extract other data, but snapshot.val, dont work.

  this.productos = af.database.list('/productos/', {
    query: {
      orderByChild: 'categoria',
      equalTo: this.catnombre
    }, preserveSnapshot:true
    });

    this.productos.subscribe(snapshot => {

         snapshot.forEach(snapshot => {

           console.log(snapshot.key);
           this.idproductos.push(snapshot.key); 

         });
         console.log(this.idproductos);
    });

1 Answer 1

2

All you need to do is

this.productos = af.database.list('/productos/', {
    query: {
      orderByChild: 'categoria',
      equalTo: this.catnombre
    })
  .map(products => products.map(product => product.$key));

The result will be an observable of arrays of keys. Now you can subscribe it to or do whatever else you want to.

this.productos.subscribe(keys => console.log("keys are", keys));

If AngularFire, and things like FirebaseListObservable, are used correctly, you don't need to worry about snapshots, or taking their val(), or doing forEach on them, or taking elements and putting them onto your own array. The FirebaseListObservable is an observable of arrays. Simply map it to create other observables, as we have done above to create an observable of arrays of keys, or subscribe to it to get the underlying data.

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

3 Comments

The problem is that i need this array for others fnctions, i dont know how read a observable.
i suggest reading, or re-reading, a good tutorial on observables. You "read an observable" by subscribing to it, and then doing things you want to do with the data in the handler you pass to subscribe. One thing you can do in the subscribe handler is store a copy of the data somewhere, which is fine, as long as you remember that the data won't be available until the observable starts firing--in the case of AngularFire, until the data has been retrieved.
Ah ok, I understand, i will try, thamks you very much.

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.