user2970483
user2970483

Reputation: 343

Subscription' is not assignable to type

I'm having a problem getting a service to display results on page. The error is the subscribing method returns a subscription type and I'm stuck trying to get it to a products array. The products are in a json file.

Setup: I'm trying to learn Angular 2 by going through a tutorial. The tutorial is dated and I'm using the latest version of angular (ng -v = @angular/cli: 1.4.2). I used ng new and ng generate to setup app.

product-list.component.ts

export class ProductListComponent implements OnInit {

  pageTitle = 'Product List';
  imageWidth = 50;
  imageMargin = 2;
  showImage = false;
  listFilter = '';
  products: IProductList[];
  subscription: Subscription;
  errorMessage = '';

  constructor(private _productListService: ProductListService) {
  }

  ngOnInit() {
**// ERROR - 'Subscription' is not assignable to type 'IProductList[]'**
    this.products = this._productListService.getProducts()  // ******** ERROR ******
      .subscribe(
        products => this.products = products,
        error => this.errorMessage = <any>error);
  }

product-list.service.ts

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';

import { IProductList } from './product-list';


@Injectable()
export class ProductListService {
  private _productListUrl = 'api/product-list/product-list.json';

  constructor(private _http: Http) { }

  getProducts(): Observable<IProductList[]> {
    return this._http.get(this._productListUrl)
            .map((response: Response) => <IProductList[]>response.json())
            .do(data => console.log('All: ' + JSON.stringify(data)))
            .catch(this.handleError);
  }

  private handleError(error: Response) {
    console.error(error);
    return Observable.throw(error.json().error || 'Server Error');
  }
}

product-list.component.html

    <tr *ngFor='let product of products | async | productFilter: listFilter' >
      <td>
        <img *ngIf='showImage' [src]='product.imageUrl' [title]='product.productName' [style.width.px]='imageWidth' [style.marging.px]='imageMargin'>
      </td>
      <td>{{product.productName}}</td>
      <td>{{product.productCode | lowercase }}</td>
      <td>{{product.releaseDate}}</td>
      <td>{{product.price | currency:'USD':true:'1.2-2' }}</td>
      <td><app-ai-star [rating] = 'product.starRating'
           (ratingClicked)='onRatingClicked($event)'></app-ai-star></td>
    </tr>

Upvotes: 5

Views: 9824

Answers (2)

Vishan
Vishan

Reputation: 57

The value of this.products is assigned in subscribe(). You can ignore the returned subscription object as in code snippet below.

ngOnInit() {
    this._productListService.getProducts() 
      .subscribe(
        products => this.products = products,
        error => this.errorMessage = <any>error);
  }

Upvotes: 0

Z. Bagley
Z. Bagley

Reputation: 9260

The problem is that you want to make your subscription = to the getProducts() call.

ngOnInit() {
    this.subscription = this._productListService.getProducts() // subscription created here
      .subscribe(
        products => this.products = products, // value applied to products here
        error => this.errorMessage = <any>error);
  }

Upvotes: 6

Related Questions