类型错误:无法读取未定义的角度 7 的属性“hasOwnProperty”

Vai*_*wad 6 build node.js server-side-rendering angular

我已经使用 command 完成了项目构建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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Ric*_*evi 1

我不确定这里使用的代码行或对象类型是什么但是,通常如果您收到此错误 - 那么您可能需要在使用变量之前检查它们。

在 Angular7 中,您可能使用 TypeScript(?),因此 - 您正在使用类似的东西export class ..,在这种情况下 hasOwnProperty 不存在(在interface / type- 您确实有。

在打字稿中,您有各种选项可以在使用之前首先检查是否有值......例如:

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);
Run Code Online (Sandbox Code Playgroud)

等等..等等..