NodeJS TypeError:无法读取未定义的angular 7的属性“hasOwnProperty”

ghhaqwfi  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(110)

我使用ng build --prod命令完成了项目构建。它生成成功构建,但此构建无法在实时服务器上运行,它给出以下错误:-

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)

3lxsmp7m

3lxsmp7m1#

我不确定这里使用的代码行或对象类型是什么,但是,通常如果您收到此错误-那么您可能需要在使用它们之前检查变量。
在Angular 7中,你可能会使用TypeScript(?),这样的话,您使用的是类似export class ..的东西,在这种情况下,hasOwnProperty不存在(在interface / type上,您确实存在。
在typescript中,你有各种各样的选项来在使用它之前首先检查你是否有一个值。例如:

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);

等等等等

相关问题