2017-12-31 54 views
2

iは、コンソールで次のエラーを取得する:角度TSエラーTS1109

webpack: Compiling... 10% building modules 0/1 modules 1 active ...p/shoppinglist2/src/app/app.module.tsERROR in src/app/meal.service.ts(46,56): error TS1109: Expression expected.

Date: 2017-12-31T09:55:50.224Z
Hash: 8003d6d8f334085afa7f Time: 267ms chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] chunk {main} main.bundle.js (main) 73.1 kB [initial] [rendered] chunk {polyfills} polyfills.bundle.js (polyfills) 558 kB [initial] chunk {styles} styles.bundle.js (styles) 456 kB [initial] chunk {vendor} vendor.bundle.js (vendor) 11.3 MB [initial]

webpack: Compiled successfully.

私は問題が表示されません。このコードは、https://angular.io/tutorial/toh-pt6のAngular HTTPの例に基づいています。 meal.service.tsから私は5.0.0 ^アンギュラ使用しています 、rxjs^5.5.2

コード:

import { Injectable }    from '@angular/core'; 
import { Observable }    from 'rxjs/Observable'; 
import { catchError, map, tap } from 'rxjs/operators'; 
import { of }      from 'rxjs/observable/of'; 
import { HttpClient, HttpHeaders, 
     HttpParams }    from '@angular/common/http'; 

import { MessageService }   from './message.service'; 
import { Meal, oneMeal, Meals } from './meal'; 

const httpOptions = { 
    headers: new HttpHeaders({'Content-Type': 'application/json'}) 
}; 

@Injectable() 
export class MealService { 

    constructor(
    private http: HttpClient, 
    private messageService: MessageService 
) { }; 

    private mealUrl = 'http://192.168.178.11:1337/api/meal'; 

    private log(message: string) { 
    this.messageService.add('MealService: ' + message); 
    }; 

    private handleError<T> (operation = 'operation', result?: T) { 
    return (error: any): Observable<T> => { 
     console.error(error) 
     this.log(`${operation} failed: ${error.message}`); 
     return of(result as T); 
    }; 
    } 

    getMeals (page:number): Observable<Meals> { 
    let httpParams = new HttpParams().set('where', '%') 
     .set('orderBy', 'meal_id') 
     .set('page', page.toString()) 
     .set('items', '10'); 
    //this.messageService.add('Debug: ' + httpParams); 
    return this.http.get<Meals>(this.mealUrl, { params: httpParams }) 
     .pipe(
     tap(meals => this.log(`fetched ${meals.count} entries`)), 
     catchError(this.handleError('getMeals', <Meals>)) // Line 46: the Error 
    ); 
    }; 

    getMeal (id:number): Observable<oneMeal> { 
    let httpParams = new HttpParams().set('id', id.toString()); 
    //this.messageService.add(`MealService: fetched meal_id=${id}`); 
    return this.http.get<oneMeal>(this.mealUrl, { params: httpParams }) 
     .pipe(
     tap(meal => this.log(`fetched ${meal.data[0].meal_name}`))//, 
     //catchError(this.handleError('getMeal', <oneMeal>)) 
    ); 
    } 
} 

答えて

4

あなたがこれを取得 ように、あなたは間違った方法で、一般的なメソッドを呼び出しています -

ERROR in src/app/meal.service.ts(46,56): error TS1109: Expression expected

間違った方法

catchError(this.handleError('getMeals', <Meals>)) // Line 46: the Error 

正しい方法

catchError(this.handleError<Meals>('getMeals')) 

getMeals (page:number): Observable<Meals> { 
    let httpParams = new HttpParams().set('where', '%') 
     .set('orderBy', 'meal_id') 
     .set('page', page.toString()) 
     .set('items', '10'); 
    //this.messageService.add('Debug: ' + httpParams); 
    return this.http.get<Meals>(this.mealUrl, { params: httpParams }) 
     .pipe(
     tap(meals => this.log(`fetched ${meals.count} entries`)), 
     catchError(this.handleError<Meals>('getMeals')) // Line 46: the Error 
    ); 
    }; 
+0

私がそうするならば、私はこのエラーを取得する:SRC /アプリ/ meal.service.ts(43,5)で 'ERROR:エラーTS2322:タイプ '観察可能<{} | Meals>は' '観測' を型に代入できません。 タイプ '{} | 「食事」は「食事」に割り当てられません。 タイプ '{}'はタイプ '食事'に割り当てられません。 プロパティ 'count'がタイプ '{}'にありません。 src/app/meal.service.ts(46,56):エラーTS2345: '食事のタイプ'の引数が '食事'タイプのパラメータに割り当てられません。 'typeof Meals'タイプの 'count'プロパティがありません。 ' – Sven

+0

@seven:答えを更新しました –

0

あなたは、あなたはパラメータを受け取ることを期待しています気づくために参照さドキュメントのhandleErrorの機能をチェックすると、

private handleError<T> (operation = 'operation', result?: T) { 
return (error: any): Observable<T> => { 

    // TODO: send the error to remote logging infrastructure 
    console.error(error); // log to console instead 

    // TODO: better job of transforming error for user consumption 
    this.log(`${operation} failed: ${error.message}`); 

    // Let the app keep running by returning an empty result. 
    return of(result as T); 
}; 
} 

しかし、あなたのコードでは、handleError関数を間違って使用しています。

この得る理由です
getMeals (page:number): Observable<Meals> { 
    let httpParams = new HttpParams().set('where', '%') 
     .set('orderBy', 'meal_id') 
     .set('page', page.toString()) 
     .set('items', '10'); 
    //this.messageService.add('Debug: ' + httpParams); 
    return this.http.get<Meals>(this.mealUrl, { params: httpParams }) 
     .pipe(
     tap(meals => this.log(`fetched ${meals.count} entries`)), 
     catchError(this.handleError('getMeals', <Meals>)) // Line 46: the Error 
    ); 
    }; 

- のsrc /アプリ/ meal.service.ts(46,56)でERROR:エラーTS1109:式が期待されます。 だから、これを試してみてください - 助け

catchError(this.handleError< Meals >('getMeals'))

希望。