使用Angular 2&的map()错误打字稿

时间:2016-12-06 14:42:19

标签: angular

我有一个Angular 2应用程序,我已经实现了一个管道来基本上检查一些空值。问题是当我使用map()时出现以下错误:

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;
        }
}

我觉得代码还可以,你可以看看我在这里写的一个例子: link

知道我的代码是怎么回事吗? 谢谢!

1 个答案:

答案 0 :(得分: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 || '-'
          })
      );
   }
}