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

Returning List only once using .take(1) results in error #47

Open
JaneDawson opened this issue Jul 13, 2017 · 6 comments
Open

Returning List only once using .take(1) results in error #47

JaneDawson opened this issue Jul 13, 2017 · 6 comments

Comments

@JaneDawson
Copy link

When I return a Firebase List Object only once like this: return this.db.list('/items').take(1); I receive the following error in VS Studio Code:

[ts]
The 'this' context of type 'AfoListObservable<any[]>' is not assignable to method's 'this' of type 'Observable<any[]>'.
Types of property 'lift' are incompatible.
Type '(operator: Operator<any[], R>) => Observable<any[]>' is not assignable to type '(operator: Operator<any[], R>) => Observable'.
Type 'Observable<any[]>' is not assignable to type 'Observable'.
Type 'any[]' is not assignable to type 'R'.

However, everything seems to compile fine.

Returning an object only once (return this.db.object('/items').take(1);) works fine.
When I use AngularFire2 instead, returning a list only once like this works fine.

@adriancarriger
Copy link
Owner

Hi @JaneDawson, thanks for spotting this issue! It looks like the types aren't matching up correctly.

Workaround

As a quick workaround just change your list's type to use any when you're using take(1).

Before

groceries: AfoListObservable<any[]>;

After

groceries: any;

Help wanted

Obviously this is not an ideal solution so anyone interested in helping is welcome to make a PR!

@JaneDawson
Copy link
Author

Hi @adriancarriger,

thanks for the workaround. However, I'm not sure how to implement it, properly.

Currently I have:

getAllOffersOnce(){
    return this.db.list('/active_offers/').take(1);
  }

This results in the error as posted above.
And I still have the same error when doing as suggested:

export class OfferService {
list: any;

  constructor( public db: AngularFireOfflineDatabase ) { }

  getAllOffersOnce(){
    this.list = this.db.list('/active_offers/').take(1);
    return list;
  }

@adriancarriger
Copy link
Owner

Hi @JaneDawson, I made a quick demo (with code) to better explain the workaround.

Basically you can just use this for your getAllOffersOnce method:

getAllOffersOnce() {
  return (<any>this.afoDatabase.list('groceries')).take(1);
}

The <any> is what allows it to avoid the type checking.

Again, I'm hopeful that this workaround won't be required in the future.

@JaneDawson
Copy link
Author

Hi @adriancarriger: Thank you for your patience and the further explanation. It's working like this.

@adriancarriger
Copy link
Owner

Awesome, glad to hear! 👍

@Ross-Rawlins
Copy link

I tried this exact code and my observable is still firing twice. has there been some changes?

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

No branches or pull requests

3 participants