简体   繁体   English

错误:'未处理的 Promise 拒绝:TypeError:无法读取未定义的属性

[英]ERROR: 'Unhandled Promise rejection: TypeError: Cannot read properties of undefined

Getting this error when run ng test.运行 ng test 时出现此错误。

TypeError: Cannot read properties of undefined (reading 'id') TypeError:无法读取未定义的属性(读取“id”)

  this.slotSvc.getFiscalWeek(startDate, 1)
      .then(function (data) {
          const inductionYear = (data[0].id).split('-')[1];
          const shopId = slot.shop.id;
          const ohLines = [];
          this.slotSvc.getOlLinesForShop(shopId, inductionYear)
              .subscribe(function (d) {
                  _.each(d, function (dataOL) {
                      let slotLineType;
                      if (slot.linetype) {
                          slotLineType = slot.linetype;
                      } else {
                          slotLineType = slot.assignedAssetInfo.lineTypes[0];
                      }
                      if (dataOL.lineTypes[0] === slotLineType && dataOL.name.includes(slot.assetType)) {
                          const olJson = { 'id': dataOL.id, 'displayName': dataOL.displayName };
                          ohLines.push(olJson);
                      }
                  }.bind(this));

Seems like you are accessing id props from data that does not exists.好像您正在从不存在的数据中访问 id 道具。 Try to monitor data params and log data if it contains id prop or not.尝试监控数据参数并记录数据是否包含 id prop。

you can do like this const inductionYear = (data[0]?.id).split('-')[1];你可以这样做 constductionYear = (data[0]?.id).split('-')[1];

after putting question mark it will not give any undefined error.加上问号后,它不会给出任何未定义的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的拒绝 (TypeError):无法读取未定义的属性(读取“错误”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'error') 反应:未处理的拒绝(TypeError):无法读取未定义的属性(读取“错误”) - React : Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'error') 未处理的承诺拒绝:TypeError:无法读取未定义的属性“用户名” - Unhandled promise rejection: TypeError: Cannot read property 'username' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性(读取“哈希”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'hash') 可能未处理的拒绝 [3] 类型错误:无法读取未定义的属性(读取“原型”) - Potentially unhandled rejection [3] TypeError: Cannot read properties of undefined (reading 'prototype') 未处理的拒绝 (TypeError):无法读取未定义的属性(读取“长度”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'length') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“状态”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'state') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“值”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'value') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“数据”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'data') 未处理的拒绝(TypeError):无法读取未定义的属性(读取“参数”) - Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'params')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM