1

I have pagination working in my Angular 2 app but am running into an issue where I'm getting an error re: the async pipe:

Invalid argument '' for pipe 'AsyncPipe'

In the research I've done re: this error, it usually has to do with the fact that the async pipe expects an observable. This is confusing to me, because I am using an observable, so I'm not sure what the issue is.

First, here's my relevant view code:

<tr *ngFor="let record of records | async | paginate: { id: 'stream', itemsPerPage: 15, currentPage: page, totalItems: total }">

This component is using an Input() from another component, like this:

@Input() records = [];

And here's "records" from that other component, which is an observable I'm subscribing to OnInit:

ngOnInit() {
   this.streamService.getBySection('section1')
        .subscribe(resRecordsData => this.records = resRecordsData,
        responseRecordsError => this.errorMsg = responseRecordsError);
}

What am I missing here? Do I need to explicitly declare this as type observable somewhere?

3
  • Presumably you mean to resolve the observable, paginate then iterate over the result? In which case let record of (records | async | paginate: { ... })". But it appears that the observable is already resolved, by the .subscribe, in your class, and not exposed to the template as an observable. Commented Mar 7, 2017 at 22:08
  • How is that different from what I already have? Commented Mar 7, 2017 at 22:10
  • The parentheses clarify the order of operations, which can sometimes go awry. But the problem you have is that records doesn't appear to be an observable. Commented Mar 7, 2017 at 22:11

1 Answer 1

4

Async pipe expect an observable, not the resolved data.

ngOnInit() {
   this.records = this.streamService.getByStage('section1');
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, so I guess my thought is, is the "async" pipe redundant in this scenario then? No need to use it if I've already returned the result of the observable. Right?
That is correct. You can just remove the async, if you're not binding to an Observable, but a plain object.
Okay, makes sense. Thanks.

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.