omkar musale
omkar musale

Reputation: 118

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

im working on angular ionic 3 and when im click on a certain button im getting this error can anyone please help me determine this ?

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
      }

line 3rd starts from "for" is line 66
error

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

Upvotes: 1

Views: 8698

Answers (3)

Syed Ismail Quadri
Syed Ismail Quadri

Reputation: 1

I was also facing the same error while working on a news app, which fetches data from external API (NEWS API). If you are working on same kind of thing,

you might have exceeded the API request limit.

To resolve this error just wait for 12 or 24 hours, your API requests get restored or upgrade to a paid plan.

Copy and paste the link of "Failed to load resource" in new tab to check your API status, you may get:

{"status":"error","code":"rateLimited","message":"You have made too many requests recently. Developer accounts are limited to 100 requests over a 24 hour period (50 requests available every 12 hours). Please upgrade to a paid plan if you need more requests."}

Upvotes: 0

Delwyn Pinto
Delwyn Pinto

Reputation: 612

The issue is in this line for (let i = 0; i < this.agentdetail.branchDetails.length; i++).

When the loop is being executed, there is no value in the variable agentdetail. Most likely some code before this fails to assign any value to that variable. Debug the code to figure this out.

Alternatively, you can use the check mentioned by @sangar-lal in his answer.

Upvotes: 0

Sangar Lal
Sangar Lal

Reputation: 50

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
      }
}

use like this it will work because this.agentdetail.branchDetails may be Empty

Upvotes: 1

Related Questions