【问题标题】:Angular - multiselect-dropdown - TypeError: Cannot read property 'length' of undefinedAngular-多选下拉菜单-TypeError:无法读取未定义的属性“长度”
【发布时间】:2025-11-25 14:10:01
【问题描述】:

  counter: number = 0;
  getDatatypes(){
    if(this.counter == 0) {
      if(this.appId != 0)
      {
        if(undefined != this.datatypes && this.datatypes.length)
          for (let i = 0; i < this.datatypes.length; i++) {
            this.applicationDataType = new ApplicationDataType(this.route.snapshot.params.id, this.datatypes[i].dataTypeId, this.datatypes[i].description, false);
            let datatype = this.checkedDatatypes.find(y => y.description === this.datatypes[i].description);
            if (datatype) {
              this.applicationDataType.checked = true;
              this.applicationDataTypes.push(this.applicationDataType);
            } else {
              this.applicationDataType.checked = false;
              this.applicationDataTypes.push(this.applicationDataType);
            }
        }
      } else {
        for(let i = 0; i < this.datatypes.length; i++){
          this.applicationDataType = new ApplicationDataType(this.route.snapshot.params.id, this.datatypes[i].dataTypeId, this.datatypes[i].description, false);
          this.applicationDataTypes.push(this.applicationDataType);
        }
      }
      this.counter ++;
    }
  }

线

if(undefined != this.datatypes &amp;&amp; this.datatypes.length)

是给TypeError: Cannot read property 'length' of undefined的人。

这是我得到的控制台错误

这些错误对用户不可见,也不影响功能。我已经尝试了所有方法,但是前端多选下拉列表中的数据类型一直在消失。我尝试过初始化数据类型:Datatype[] = [],用if(this.datatypes &amp;&amp; this.datatypes.length) 包装我的 for 循环,并使用 ?。这些只是在运行前端时使多选下拉列表为空。

【问题讨论】:

    标签: javascript angularjs angular


    【解决方案1】:

    似乎this.datatypes 不是一个数组或它的空值,你可以简单地用? 忽略这个错误。

    请将其更改为if(this.datatypes?.length&gt;0)

    【讨论】:

    • 我试过了,但它使多选下拉列表消失了。
    • 安全运算符来自 typescript 3.7,我认为 Angular 使用 typescript 3.1。通常我使用if (this.datatypes &amp;&amp; this.datatypes.length)。这包括是 this.datatypes 是 null
    • @Eliseo 我也尝试用它来包装我的 for 循环,但它使下拉列表为空。
    • 您在写问题时是否忘记了 if(undefined != this.datatypes....) 中的 {
    • 不,我没有@Eliseo
    【解决方案2】:

    您可以尝试如下。初始化 dataTypes 数组,然后用 forEach 替换 for 循环,如下所示

    counter: number = 0;
      datatypes = [];
      getDatatypes(){
        if(this.counter == 0) {
          if(this.appId != 0)
          {
            this.datatypes.forEach(dtype => {
              this.applicationDataType = new ApplicationDataType(this.route.snapshot.params.id, dtype.dataTypeId, dtype.description, false);
                let datatype = this.checkedDatatypes.find(y => y.description === dtype.description);
                if (datatype) {
                  this.applicationDataType.checked = true;
                  this.applicationDataTypes.push(this.applicationDataType);
                } else {
                  this.applicationDataType.checked = false;
                  this.applicationDataTypes.push(this.applicationDataType);
                }
            })
          } else {
            this.datatypes.forEach(dtype => {
              this.applicationDataType = new ApplicationDataType(this.route.snapshot.params.id, dtype.dataTypeId, dtype.description, false);
              this.applicationDataTypes.push(this.applicationDataType);
            })
          }
          this.counter ++;
        }
      }
    

    【讨论】:

    • 我初始化了数据类型。 export class ApplicationComponent implements OnInit { username: string; appId: number; dataTypeId: number; application: Application; appData: AppData; appDatas: AppData[]; datatypes: Datatype[];我尝试了你给我的代码,但我删除了你的datatypes = [];,因为它在导出类下给了我错误,但它给了我core.js:4352 ERROR TypeError: Cannot read property 'forEach' of undefined
    • 我收到core.js:4352 ERROR TypeError: Cannot read property 'forEach' of undefined
    • 什么错误数据类型:当你在类中声明它时,Datatype[] 给出了什么?必须声明和初始化数据类型才能使用它。它已经宣布了吗?
    • 如果可能的话,您能否显示声明数据类型的位置以及分配其值的位置的代码?谢谢
    • 作为临时修复,我建议现在用“this.datatypes && this.datatypes.forEach”替换“this.datatypes.forEach”。但是我们需要找到声明数据类型的方式和位置并在那里修复它。
    最近更新 更多