如何修复Angular中的“ ERROR TypeError:无法读取未定义的属性'forEach'”?

时间:2019-08-01 05:19:17

标签: html angularjs typescript

我遇到以下错误,

  

错误TypeError:无法读取未定义的属性'forEach'。

我正在尝试从数据库中获取数据并在Angular中建立模型以保存相同的数据。

我试图制作模型,但是失败了。

在这里,loadConversation函数从数据库中获取数据。其余的是试图保存数据的模型。

    loadConversation() {
        this.conversationsService.get('http://172.26.90.49:9000/api/v1/conversation/conversation/?conversation_id=1')
          .subscribe((res: any) => {
            this.conversation = new Conversation(res);
          });
      }


    export class Conversation {

        title: string;
        final_conclusion_message: string;
        gender: string;
        dialogs!: Dialog[];


        constructor(data: any) {
            data = data || {};

            this.title = data.title;
            this.final_conclusion_message = data.final_conclusion_message;
            this.gender = data.gender;
            this.dialogs = [];
            data.dialog_options.forEach((q: any) => {
                this.dialogs.push(new Dialog(q));
            });
        }



    }

export class Dialog {
    id!: number;
    message!: string;
    dialog_images!: any[];
    dialog_has_options!: DialogOptions[];


    constructor(data: any) {
        data = data || {};
        this.id = data.id;
        this.message = data.message;
        this.dialog_images = data.dialog_images;
        this.dialog_has_options = [];
        data.dialog_has_options.forEach((q: any) => {
            this.dialog_has_options.push(new DialogOptions(q));
        });



    }



}


export class DialogOptions {
    option!: {
        id: number;
        message: string;
        option_images: any;
    };
    upcoming_dialog!: number;
    wrong_option_message!: string;
    loopback!: boolean;
    is_last!: boolean;

    constructor(data: any) {
        data = data || {};

        this.option.id = data.option.id;
        this.option.message = data.option.message;
        this.option.option_images = data.option.option_images;
        this.upcoming_dialog = data.upcoming_dialog;
        this.wrong_option_message = data.wrong_option_message;
        this.loopback = data.loopback;
        this.is_last = data.is_last;

    }

}

我要解决该错误。

0 个答案:

没有答案