错误TypeError:“ _ co.category.category_type未定义”

时间:2019-04-19 19:09:38

标签: angular typescript

我有两个模型CategoryTypesCategories,每个类别类型都有多个类别。这是我的两个模型。

Categories模型

import { Categories } from '../../categories/Mode/Categories'
export class CategoryType {
    _id: string;
    title: string;
    description: string;
    slug: string;
    user_id: string;
    created_at: string;
    updated_at: string;
    status: string;
    categories: Categories[];
}

CategoryType模型

import { Categories } from '../../categories/Model/Categories'
export class CategoryType {
    _id: string;
    title: string;
    description: string;
    slug: string;
    user_id: string;
    created_at: string;
    updated_at: string;
    status: string;
    categories: Categories[];
}

数据是

{
    "_id": "5cb8c22ea7d8ec2c30849c3f",
    "title": "fdsffds",
    "description": "fdsfdsfdsf",
    "slug": "dsfdsfdsf",
    "categorytype_id": "5cb8c21da7d8ec2c30849c3e",
    "user_id": "fdsfdsfsdf",
    "status": false,
    "created_at": "2019-04-18T18:30:07.064Z",
    "updated_at": "2019-04-19T17:36:47.052Z",
    "__v": 0,
    "category_type": {
        "_id": "5cb8c21da7d8ec2c30849c3e",
        "title": "sgsdgdsg",
        "description": "dsgdsgds",
        "slug": "gdsgdsgds",
        "user_id": "gdsgdsg",
        "status": true,
        "created_at": "2019-04-18T18:29:49.544Z",
        "updated_at": "2019-04-18T18:29:49.544Z",
        "__v": 0,
        "categories": null,
        "id": "5cb8c21da7d8ec2c30849c3e"
    },
    "id": "5cb8c22ea7d8ec2c30849c3f"
},

通过{{category.category_type.title}}获取价值,将正确输出错误ERROR TypeError: "_co.category.category_type is undefined"。我该如何解决。

1 个答案:

答案 0 :(得分:2)

根据您发布的JSON,接口定义应为

  export interface CategoryType {
        _id: string;
        title: string;
        description: string;
        slug: string;
        user_id: string;
        status: boolean;
        created_at: Date;
        updated_at: Date;
        __v: number;
        categories?: any;
        id: string;
    }

    export interface Category {
        _id: string;
        title: string;
        description: string;
        slug: string;
        categorytype_id: string;
        user_id: string;
        status: boolean;
        created_at: Date;
        updated_at: Date;
        __v: number;
        category_type: CategoryType;
        id: string;
    }

}

JSON2TS

生成
相关问题