TypeError:无法读取null angular 2的属性“id”

时间:2016-05-30 14:56:57

标签: javascript typescript angular gulp

angular2.dev.js:24821 TypeError: Cannot read property 'id' of null
at http://192.168.20.21:8080/js/menu/menu-page.component.js:88:50
at Array.find (http://192.168.20.21:8080/libs/node_modules/es6-shim/es6-shim.js:1143:20)
at http://192.168.20.21:8080/js/menu/menu-page.component.js:87:59


menu-page.component.js code is below.

import {HTTP_PROVIDERS} from 'angular2/http';
import {Component} from 'angular2/core';
import {HeaderMenuComponent} from '../shared/directives/headermenu.directive2';
import {CategoryComponent} from './category.component';
import {RestOptions,RestService} from '../shared/services/rest.service';
import {MenuComponent} from './menu.compoment';
import {MenuService} from './menu.service';
import {CartService} from '../shared/services/cart.service';


@Component({
selector: 'menupage',
providers: [
    HTTP_PROVIDERS,
    RestService,
    RestOptions,
    MenuService
],
directives: [HeaderMenuComponent,CategoryComponent ,MenuComponent],
template: `
    <headermenu2 [cart]="cartitems"></headermenu2>
    <div class="search-box2">
        <i class="fa fa-search" aria-hidden="true"></i>
        <input type="text" class="input2" #input placeholder="Enter your location"/>
        <i class="fa fa-crosshairs" aria-hidden="true"></i>
    </div>
    <label class="switch">
    <span class="veg">
    Veg  </span>
        <input type="checkbox" checked (change)="switched()">
        <div class="slider round"></div>
    <span class="non-veg"> Everything </span>
    </label>
    <category [items]="categoryItems" (change)="change($event)"></category>
    <div [hidden]="loaded" class="spinner"></div>
    <menu class="menuPage"  [items]="currentMenuItems" (addToCart)="addToCart($event)"></menu>
`
 })
 export class MenuPageComponent {
 menuitems :any;
 cartitems: any;
 categoryItems:any;
 pending: any;
 currentMenuItems:any;
 switchVeg:any;

constructor(private menuService: MenuService,private cartService:CartService) {
    this.menuitems = new Array();
    this.cartitems = new Object;
    this.loaded = false;
    this.switchVeg=false;
    let length = 0;

    this.pending = cartService.fetch();
    if(this.pending !== undefined && this.pending !== null) {
        length = this.pending.length;
    }

    if(length !== 0) {
        this.cartitems.show = true;
        this.cartitems.itemCount=length;
    }else {
        this.cartitems.itemCount=0;
        this.cartitems.show = false;
    }
    let location = '';
    if(this.cartService.store!== undefined) {
        location = '&location_id='+this.cartService.store.biz_location_id;
    }
    this.menuService
            .fetch('/api/v1/order/categories/?format=json')
            .subscribe((response: any) => {
                this.categoryItems = response.objects;
                this.loaded = true;
                var res = Math.min.apply(Math, this.categoryItems.map((o:any)=> {
                    return o.sort_order;
                }));

                var obj = this.categoryItems.find((o:any)=> {
                    return o.sort_order === res;
                });

                var url = '/api/v1/order/categories/' + obj.id + '/items/?format=json' + location;
                this.menuService
                    .fetch(url)
                    .subscribe((response:any) => {
                        this.currentMenuItems = response.objects;
                        this.currentMenuItems.map((o:any)=> {
                            o.quantity = false;
                            o.categoryId = obj.id;
                            if(length  !== 0 && this.pending.length > 0 ) {
                                let t = this.pending.find((po:any)=> {
                                    return po.id === o.id;
                                });
                                if( t!== undefined) {o.quantity=t.quantity;}
                                o.categoryId = obj.id;
                            }else {
                                this.cartitems.itemCount=0;
                                this.cartitems.show = false;
                            }
                        });

                    });

                this.categoryItems.map((o:any)=> {
                    url = '/api/v1/order/categories/' + o.id + '/items/?format=json'+location;
                    this.menuService
                        .fetch(url)
                        .subscribe((response:any) => {
                            response.objects.map((obj:any)=> {
                                obj.quantity = false;
                                obj.categoryId=o.id;
                                if(length  !== 0 &&  this.pending.length > 0) {
                                    let t =  this.pending.find((po:any)=> {
                                        return po.id === obj.id;
                                    });
                                    if( t!== undefined)  {obj.quantity=t.quantity;}
                                    obj.categoryId = o.id;
                                }else {
                                    this.cartitems.itemCount=0;
                                    this.cartitems.show = false;
                                }

                            });
                            this.menuitems[o.id] = response.objects;
                        });
                });
            });
 }

 switched() {
    if(this.switchVeg===false) {
        this.switchVeg=this.currentMenuItems;
        this.currentMenuItems= this.currentMenuItems.filter(( obj:any )=> {
            return obj.food_type === '1';
        });

    }else {
        this.currentMenuItems = this.switchVeg;
        this.switchVeg = false;
    }
}

change(category:any): void {
    if(this.switchVeg !== false) {
        this.switchVeg = false;
        this.currentMenuItems = this.menuitems[category.id];
        this.switched();
    }else {
        this.currentMenuItems = this.menuitems[category.id];
    }
}
addToCart(item:any):void {
    let qty = this.cartService.add(item);
    let length =0;

    let pending = new Array();
    this.cartService.fetch();
    if(pending !== undefined) {
        length = pending.length;
    }
    if(length !== 0) {
        this.cartitems.show = true;
        this.cartitems.itemCount=length;
    }else {
        this.cartitems.itemCount=0;
        this.cartitems.show = false;
    }this.currentMenuItems[this.currentMenuItems.indexOf(item)].quantity=qty;
}

}

以上代码在本地工作但不在登台服务器上工作。我的问题是为什么角度2中的这个错误虽然在本地运行良好。由于这个错误,我无法将东西添加到购物车,无法浏览菜单链接。任何帮助将受到高度赞赏。

0 个答案:

没有答案