Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

subscription to list query always returns undefined #64

Open
matheus-fatguys opened this issue Aug 22, 2017 · 2 comments
Open

subscription to list query always returns undefined #64

matheus-fatguys opened this issue Aug 22, 2017 · 2 comments

Comments

@matheus-fatguys
Copy link

matheus-fatguys commented Aug 22, 2017

I can't unsubscribe from AfoListObservable when I query it because its subscription method always returns undefined.

I have a provider method that querys a list:

Code is bellow:

`

obterCondutorPeloUsuarioLogado(){

let user = this.auth.usuarioLogado();

if(user==null){
  return null;
}

let obs = this.afd.list("condutores", {
  query: {
    orderByChild: "usuario",
    equalTo: user.uid
  }
})
obs.subscribe(condutor=>{
        this.condutor=condutor[0];
});  
return obs;

}

`

Here is my component code subscribing to the list query:

`

let ref= this.fatguysService.obterCondutorPeloUsuarioLogado();
if(ref!=null){
if(this.loading==null){
this.loading = this.loadingCtrl.create({
content: 'Buscando condutor...'
});
}
else{
this.loading.setContent("Buscando condutor...");
}
this.loading.present().then(
_=>{
const sub =
ref.subscribe(
r=>{
sub.unsubscribe();

`

This last line always throws an error cannot read property unsubscribe of undefined.

I noticed that if I comment out the following lines, it works:

// obs.subscribe(condutor=>{
// this.condutor=condutor[0];
// });

@belavenuto
Copy link

I think it's because you are not returning the subscription on obterCondutorPeloUsuarioLogado() method.
You are subscribing to the AfoListObservable called obs, but when you return obs, you are not returning the subscription, but only the AfoListObservable reference.
To solve the problem, you should subscribe just on client method, like this:

const subscription = this.fatguysService.obterCondutorPeloUsuarioLogado().subscribe(condutor => {
  // Do whatever you need to do with data
});

After that, you'll be able to unsubscribe (subscription.unsubscribe()).

@matheus-fatguys
Copy link
Author

but it shouldn't matter if I subscribe inside the method...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants