TypeError: Cannot read property 'hasOwnProperty' of undefined angular 7
Asked Answered
C

1

6

I have done project build using command ng build --prod. It generate successfully build but this build not able to run on live server, It gives following error:-

main.0769518151819531d924.js:1 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'hasOwnProperty' of undefined
TypeError: Cannot read property 'hasOwnProperty' of undefined
    at v (main.0769518151819531d924.js:1)
    at Mf (main.0769518151819531d924.js:1)
    at t.get (main.0769518151819531d924.js:1)
    at kt (main.0769518151819531d924.js:1)
    at Pt (main.0769518151819531d924.js:1)
    at Et (main.0769518151819531d924.js:1)
    at main.0769518151819531d924.js:1
    at main.0769518151819531d924.js:1
    at Df (main.0769518151819531d924.js:1)
    at Mf (main.0769518151819531d924.js:1)
    at P (polyfills.7d30aceca8fe7a5b6974.js:1)
    at P (polyfills.7d30aceca8fe7a5b6974.js:1)
    at polyfills.7d30aceca8fe7a5b6974.js:1
    at e.invokeTask (polyfills.7d30aceca8fe7a5b6974.js:1)
    at Object.onInvokeTask (main.0769518151819531d924.js:1)
    at e.invokeTask (polyfills.7d30aceca8fe7a5b6974.js:1)
    at t.runTask (polyfills.7d30aceca8fe7a5b6974.js:1)
    at g (polyfills.7d30aceca8fe7a5b6974.js:1)
    at t.invokeTask [as invoke] (polyfills.7d30aceca8fe7a5b6974.js:1)
    at m (polyfills.7d30aceca8fe7a5b6974.js:1)

enter image description here

Catgut answered 10/2, 2019 at 7:42 Comment(6)
Try initializing the object with {} on which hasOwnProperty is appliedTorietorii
Thanks for answer but how do i find that particular object which have hasOwnProperty at the this stage, i have build my complete project and it successfully build, but after deploying the build folder, it gives above error.Catgut
Without seeing your code, I cannot suggest. But seems to be the only way to go, just cross check all your objects which are using that method. That should not take very long time.Torietorii
Sure, I'll cross check all the objects, and let you know the status. thank you.Catgut
@Amit Chigadani, I have tried to all my object with { } but still no change, their is same problem.Catgut
@VaibhavGaikwad Amit is right. Your object which is used in the page is undefined. So, that's why it cannot access hasOwnProperty. Wherever you use hasOwnProperty() function, first you should check whether that object exists or not (i.e. it is initialized or not).Census
C
1

I'm not sure what is the line of code or the object type used here But, generally if you received this error - then you probably required to check your variables before using them.

In Angular7 you probably use TypeScript (?) and by that - you're using something like export class .. and in that case hasOwnProperty doesn't exists ( on interface / type - you do have.

In typescript you have all kinds of options to first check if you have a value before you use it.... for example:

console.log(user.department?.length);
console.log(user.department !== undefined);
console.log(user.address?.country !== undefined));
console.log(user.address?.country?.toLowerCase());
console.log('department' in user);

etc .. etc..

Calebcaledonia answered 15/6, 2023 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.