为什么我的数组未定义但它实际上应该有一个对象?

Why is my Array undefined but it actually should have an Object?

我正在尝试编写一个应该包含项目的一些对象的数组,但我总是收到一个错误消息,名为:"this.allgmArray is undefined" 但实际上不是:

  allgmArray: DialogBook[];
[...]
load(){

    for(var i = 0; i < this.service.allgmTempArray.length; i++) {
      if(this.service.allgmTempArray[i].type == this.service.tempGroup.get('type').value){
        var fullName = this.service.tempGroup.get('fullName').value;
        console.log(fullName) //works fine
        var type = this.service.allgmTempArray[i].type;
        console.log(type) //works fine
        var device = this.service.allgmTempArray[i].device;
        console.log(device) //works fine
        var date_from = this.service.tempGroup.get('date_from').value;
        console.log(date_from) //works fine
        var date_to = this.service.tempGroup.get('date_to').value;
        console.log(date_to) //works fine
        var date_from_og = this.service.tempGroup.get('date_from_og').value;
        console.log(date_from_og) //works fine
        var date_to_og = this.service.tempGroup.get('date_to_og').value;
        console.log(date_to_og) //works fine
        var obj = {
          fullName: fullName,
          type: type,
          device: device,
          date_from: date_from,
          date_to: date_to,
          date_from_og: date_from_og,
          date_to_og: date_to_og
        }
        console.log(obj) //<- Comes out the right obj
        this.allgmArray.push(obj) //<- I think heres the problem
      }
    }
    console.log(this.allgmArray) //Error: this.allgmArray is undefined
//And "DialogBook" interface look like:
export interface DialogBook {
    fullName: String;
    device: String;
    type: String;
    date_from: String;
    date_to: String;
    date_from_og: Date;
    date_to_og: Date;
}

所以我真的不知道问题出在哪里,需要一些帮助

allgmArray: DialogBook[];

是定义,不是实例化。

allgmArray: DialogBook[] = [];

应该可以解决您的问题。