Mongodb 未定义对象值上的Angular2 FormGroup patchValue错误

Mongodb 未定义对象值上的Angular2 FormGroup patchValue错误,mongodb,angular,typescript,undefined,formgroups,Mongodb,Angular,Typescript,Undefined,Formgroups,我一直试图解决的问题与http请求和表单组有关。我试图在编辑页面上将现有值修补到表单中 如果数据库(mongoDB)中不存在值,则会出现错误,该值仅存在于已添加到的对象上。下面的代码是我的请求、formgroup和patchValue this.takeawayService.getByName(this.id).subscribe(data => { this.takeaway = data.takeaway[0]; con

我一直试图解决的问题与http请求和表单组有关。我试图在编辑页面上将现有值修补到表单中

如果数据库(mongoDB)中不存在值,则会出现错误,该值仅存在于已添加到的对象上。下面的代码是我的请求、formgroup和patchValue

        this.takeawayService.getByName(this.id).subscribe(data => {
            this.takeaway = data.takeaway[0];
            console.log(this.takeaway);
            this.formData = this.fb.group({
                address : ['', Validators.required],
                city : ['', Validators.required],
                county : ['', Validators.required],
                postCode : ['', [Validators.required,
                    Validators.pattern('[A-Za-z]{1,2}[0-9Rr][0-9A-Za-z]? [0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}')]],
                bio : ['', Validators.required],
                image : '',
                _id : '',
                cuisines: [],
                isPublic: [false, Validators.required],
                'openingHours': fb.group({
                    'monday': fb.group({
                        opened: [false],
                        open: [''],
                        close: ['']
                    })

                })


            });

            this.formData.patchValue({
                address: this.takeaway.address,
                city: this.takeaway.city,
                county: this.takeaway.county,
                postCode: this.takeaway.postCode,
                bio: this.takeaway.bio,
                image: this.takeaway.image,
                _id: this.takeaway._id,
                cuisines: this.takeaway.cuisines,
                isPublic: this.takeaway.isPublic,
                openingHours: {
                    monday:{
                        opened: this.takeaway.openingHours.monday.opened,
                        open: null,
                        close: null
                    }
                }

            });
尝试修补请求未返回的值时收到的错误是“TypeError:undefined不是对象(计算“\u this.takeaway.openingHours.monday”)

我曾尝试使用ternaries来解决这个问题,但运气不佳(openingHours.monday.opened?openingHours.monday.opened:false)。任何帮助都将是惊人的。我试图寻找任何与我的问题相似的东西,但运气不佳

编辑: 我试图通过将一些字段设置为可选字段来使用formgroup模板,但运气不佳。该值仍未定义

export interface Takeaway {
   address: String,
   city: String,
   county: String,
   postCode: String,
   bio: String,
   image: String,
   _id: String,
   cuisines: [String],
   isPublic: Boolean,
   openingHours?: {
       monday?: {
           opened?: Boolean,
           open?: Date,
           close?: Date,

       }
   }

是否检索此.takeaway.openingHours.monday的值?如果不是,你可以试试这个。外卖。开张时间。星期一?this.takeaway.openingHours.monday.opened:false@Dimuthu我可以让它工作,如果我检查一下,看看这个。外卖。开放小时?。我担心这不是正确的方法。我还尝试使用一个界面来显示我的预期数据(如上面的编辑中所示),但没有成功。