代码之家  ›  专栏  ›  技术社区  ›  Стас Рябцев

错误类型错误:无法读取null pipe async的属性“toString”

  •  1
  • Стас Рябцев  · 技术社区  · 7 年前

    我在创建自定义管道时遇到了这个错误,所有操作都很好,即使是从数据库中获取的文本,我也能很好地显示出来,就像我希望使用管道一样。

    enter image description here

    当我粘贴非异步代码时

    <a href="tel:{{contacts?.phoneNumber}}" class="number">{{'88004334433' | phone}}</a>
    

    一切正常,无误。但当我从数据库中获取数据时,就会发生这种错误。

    enter image description here

    如何删除此错误? 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

        import {Pipe} from '@angular/core';
    
    
    @Pipe({
        name: 'phone'
    })
    export class PhonePipe {
        transform(tel, args) {
            const value = tel.toString().trim().replace(/^\+/, '');
    
            if (value.match(/[^0-9]/)) {
                return tel;
            }
    
            let country, city, number;
    
            switch (value.length) {
                case 10: // +1PPP####### -> C (PPP) ###-####
                    country = 1;
                    city = value.slice(0, 3);
                    number = value.slice(3);
                    break;
    
                case 11: // +CPPP####### -> CCC (PP) ###-####
                    country = value[0];
                    city = value.slice(1, 4);
                    number = value.slice(4);
                    break;
    
                case 12: // +CCCPP####### -> CCC (PP) ###-####
                    country = value.slice(0, 3);
                    city = value.slice(3, 5);
                    number = value.slice(5);
                    break;
    
                default:
                    return tel;
            }
    
            if (country === 1) {
                country = '';
            }
    
            number = number.slice(0, 3) + '-' + number.slice(3);
    
            return (country + ' (' + city + ') ' + number).trim();
        }
    }
    

    组成部分ts

       import {Component, OnInit} from '@angular/core';
    import {combineLatest} from 'rxjs/observable/combineLatest';
    
    import {ContactsService} from '../../services/contacts.service';
    import {GoodsService} from '../../services/goods.service';
    import {NavigationStart, Router} from '@angular/router';
    
    @Component({
        selector: 'app-header',
        templateUrl: './header.component.html',
        styleUrls: ['./header.component.css']
    })
    export class HeaderComponent implements OnInit {
    
        contacts;
        goods: any = [];
        menu;
    
        constructor(private contactsService: ContactsService,
                    private goodsService: GoodsService,
                    router: Router) {
            router.events.subscribe(event => {
                if (event instanceof NavigationStart) {
                    this.menu = 'none';
                }
            });
        }
    
        ngOnInit() {
            combineLatest(
                this.contactsService.getContacts(),
                this.goodsService.getGoods()
            ).subscribe(data => {
                this.contacts = data[0][0];
                this.goods = data[1].find((element) => element['_id'] === '5a957797f36d280b142d1daa');
            });
        }
    
    }
    

    html

    <a href="tel:{{contacts?.phoneNumber}}" class="number">{{contacts?.phoneNumber | phone}}</a>
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   ShellNinja    7 年前

    正在为未初始化或未分配的变量调用管道。该变量在由服务调用填充之前不存在。检查管道中的输入是一个很好的习惯。

    在管道中添加以下检查,以验证变量是否有值。

    if(!tel) return "";