【问题标题】:Uncaught (in promise): TypeError: Cannot read property 'length' of undefined未捕获(承诺):TypeError:无法读取未定义的属性“长度”
【发布时间】:2020-12-04 10:31:20
【问题描述】:

我正在使用 angular ionic 3,当我点击某个按钮时,我得到了这个错误,谁能帮我确定这个?

realestateproductdetails.ts

if (localStorage.getItem('agentdetail')) {
      this.agentdetail = JSON.parse(localStorage.getItem('agentdetail'));
      for (let i = 0; i < this.agentdetail.branchDetails.length; i++) {
        this.branchCodeArray[i] = this.agentdetail.branchDetails[i].branchCode
      }

从“for”开始的第 3 行是第 66 行
错误

Runtime Error
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined


at new RealestateproductdetailsPage (realestateproductdetails.ts:66)
    at createClass (core.js:12491)
    at createDirectiveInstance (core.js:12326)
    at createViewNodes (core.js:13784)
    at createRootView (core.js:13673)
    at callWithDebugContext (core.js:15098)
    at Object.debugCreateRootView [as createRootView] (core.js:14381)
    at ComponentFactory_.create (core.js:11278)
    at ComponentFactoryBoundToModule.create (core.js:4030)
    at NavControllerBase._viewInit (nav-controller-base.js:441)
    at c (polyfills.js:3)
    at Object.reject (polyfills.js:3)
    at NavControllerBase._fireError (nav-controller-base.js:223)
    at NavControllerBase._failed (nav-controller-base.js:216)
    at nav-controller-base.js:263
    at t.invoke (polyfills.js:3)
    at Object.onInvoke (core.js:4760)
    at t.invoke (polyfills.js:3)
    at r.run (polyfills.js:3)
    at polyfills.js:3

【问题讨论】:

  • 嘿,在这里附上你的代码。
  • 先生是哪一部分?
  • 您的错误出现在realestateproductdetails.ts 66 行,您尝试从未定义的变量访问属性长度。为了修复它,您需要从该行发布完整代码
  • 好的先生发布它
  • 请阅读这篇文章。它肯定会帮助你写出一个好问题。 stackoverflow.com/help/how-to-ask

标签: angular ionic3


【解决方案1】:
if (localStorage.getItem('agentdetail')) {
      this.agentdetail = JSON.parse(localStorage.getItem('agentdetail'));
if (this.agentdetail && this.agentdetail.branchDetails && this.agentdetail.branchDetails.length > 0){
      for (let i = 0; i < this.agentdetail.branchDetails.length; i++) {
        this.branchCodeArray[i] = this.agentdetail.branchDetails[i].branchCode
      }
}

这样使用它会起作用,因为 this.agentdetail.branchDetails 可能是空的

【讨论】:

    【解决方案2】:

    问题出在 for (let i = 0; i &lt; this.agentdetail.branchDetails.length; i++)这一行。

    循环执行时,变量agentdetail中没有值。在此之前的某些代码很可能无法为该变量分配任何值。调试代码来解决这个问题。

    或者,您可以使用@sangar-lal 在他的回答中提到的检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-16
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 2020-09-24
      相关资源
      最近更新 更多