2016-12-06 9 views
1

私は基本的にいくつかのnull値をチェックするパイプを実装している角度2のアプリケーションがあります。問題は、私はmap()を使用するときに、私は以下のエラーを取得です:map()エラー2を使用して、角2&typescript

core.umd.js:2838 EXCEPTION: Uncaught (in promise): Error: Error in app/components/performances/performances.component.html:18:10 caused by: Cannot read property 'map' of undefined 
TypeError: Cannot read property 'map' of undefined 

をあなたは私のコードを見てとることができます以下:

import { Pipe, PipeTransform } from '@angular/core'; 
import {Performance} from '../common/performance'; 

@Pipe({ 
    name: 'defaultPerformanceValueIfNull' 
}) 

export class PerformanceCheckValuesPipe implements PipeTransform { 
    transform(values: Performance[]): any { 
      let performances = values.map(function(p) { 
       return Object.assign(p, 
        { 
         OneMonthBack: p.OneMonthBack || '-', 
         ThreeMonthsBack: p.ThreeMonthsBack || '-' 
        }) 
      }); 
      return performances; 
     } 
} 

私は、コードはOKだと思う、あなたは例を見てとることができますIここに書いてください: link

私のコードで何が起こっているのですか?感謝! ありがとう!

+0

マップ演算子をインポートしてみましたか?つまり、 'import 'rxjs/add/operator/map''または' import'ですべての演算子をインポートするrxjs/Rx '; ' – Bean0341

答えて

2

あなたのパイプをより頑丈にするために、このようにしてください!

入力データが未定義の最初のパイプ変換時のようですね!

export class PerformanceCheckValuesPipe implements PipeTransform { 
    transform(values: Performance[]): any { 
     if (!values || !values.length) return []; 

     return values.map(p => Object.assign(p, { 
      OneMonthBack: p.OneMonthBack || '-', 
      ThreeMonthsBack: p.ThreeMonthsBack || '-' 
      }) 
    ); 
    } 
} 
+0

ありがとう@mxiiあなたが私に言った変更でうまくいきますが、なぜ私はそれをやらなければならないのか理解していない。私は1つの場所でのみパイプを使用しています..私にとって意味をなさない。 – gon250

+1

'変換したいデータの原因は、最初の変更検出' undefined'の間です。変数が 'values:number [];'のようなものであると仮定してください。定義されておらず、http.getなどで宣言されます。これを 'values:] = [];'に変更することができます。 – mxii

関連する問題