简体   繁体   English

升级到 Angular 10 后无法读取未定义的属性“0”中的错误

[英]ERROR in Cannot read property '0' of undefined after upgrade to Angular 10

I upgraded my angular application from version 8 to version 10. Now after running ng serve I am getting below one-liner error ERROR in Cannot read property '0' of undefined This looks very abstract error.我将我的 angular 应用程序从版本 8 升级到版本 10。现在在运行 ng serve 之后,我遇到了单行错误错误,无法读取未定义的属性“0”这看起来非常抽象的错误。

I tried clearing npm cache, deleted the package.lock.json file, deleted node_modules, and install the npm packages again but nothing seems to help me. I tried clearing npm cache, deleted the package.lock.json file, deleted node_modules, and install the npm packages again but nothing seems to help me.

Below is my package.json file下面是我的 package.json 文件

{
  "name": "app-name",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@agm/core": "^1.1.0",
    "@agm/js-marker-clusterer": "^1.1.0",
    "@angular/animations": "~10.0.9",
    "@angular/cdk": "~10.1.3",
    "@angular/common": "~10.0.9",
    "@angular/compiler": "~10.0.9",
    "@angular/core": "~10.0.9",
    "@angular/forms": "~10.0.9",
    "@angular/material": "^10.1.3",
    "@angular/platform-browser": "~10.0.9",
    "@angular/platform-browser-dynamic": "~10.0.9",
    "@angular/router": "~10.0.9",
    "@types/googlemaps": "^3.39.12",
    "angular-material-badge": "^1.2.91",
    "angular-notifier": "^6.0.1",
    "azure-maps-control": "^2.0.20",
    "bootstrap": "^4.4.1",
    "crypto-js": "^4.0.0",
    "dateformat": "^3.0.3",
    "dropbox": "^5.2.1",
    "iv-viewer": "^2.0.1",
    "js-marker-clusterer": "^1.0.0",
    "mammoth": "^1.4.9",
    "ng-inline-svg": "^10.0.0",
    "ng2-image-viewer": "^3.0.5",
    "ng2-pdf-viewer": "^6.0.2",
    "ngx-doc-viewer": "1.0.0",
    "ngx-light-carousel": "1.0.20",
    "ngx-pagination": "^5.0.0",
    "ngx-perfect-scrollbar": "^9.0.0",
    "ngx-permissions": "^7.0.3",
    "rxjs": "~6.6.2",
    "rxjs-compat": "^6.6.2",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1000.5",
    "@angular/cli": "~10.0.5",
    "@angular/compiler-cli": "~10.0.9",
    "@angular/language-service": "~10.0.9",
    "@types/jasmine": "~3.5.12",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "@types/office-js": "^1.0.123",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~3.3.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.10.2",
    "tslint": "~6.1.0",
    "typescript": "~3.9.7"
  }
}

I am using node 12.15.0 version and npm version 6.13.4.我使用的是节点 12.15.0 版本和 npm 版本 6.13.4。 Please help.请帮忙。

I'm having the same issue.我有同样的问题。 I tracked down the stack trace by running ng serve in the vs code debugger and having it break on unhandled errors.我通过在 vs 代码调试器中运行 ng serve 并让它在未处理的错误上中断来跟踪堆栈跟踪。

For me the error is:对我来说,错误是:

Exception has occurred: TypeError: Cannot read property '0' of undefined
  at Object.typeToValue (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/reflection/src/type_to_value.js:45:66)
    at /workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/reflection/src/typescript.js:75:58
    at Array.map (<anonymous>)
    at TypeScriptReflectionHost.getConstructorParameters (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/reflection/src/typescript.js:57:36)
    at Object.getConstructorDependencies (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/annotations/src/util.js:29:36)
    at extractInjectableCtorDeps (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/annotations/src/injectable.js:232:72)
    at InjectableDecoratorHandler.analyze (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/annotations/src/injectable.js:72:31)
    at TraitCompiler.analyzeTrait (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/transform/src/compilation.js:346:40)
    at analyze (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/transform/src/compilation.js:298:58)
    at _loop_1 (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/transform/src/compilation.js:320:21)
    at TraitCompiler.analyzeClass (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/transform/src/compilation.js:326:35)
    at visit (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/transform/src/compilation.js:108:27)
    at visitNodes (/workingcopypath//node_modules/typescript/lib/typescript.js:18956:30)
    at Object.forEachChild (/workingcopypath//node_modules/typescript/lib/typescript.js:19189:24)
    at visit (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/transform/src/compilation.js:110:20)
    at TraitCompiler.analyze (/workingcopypath//node_modules/@angular/compiler-cli/src/ngtsc/transform/src/compilation.js:112:13)

The failing code in question is this line:有问题的失败代码是这一行:

return noValueDeclaration(typeNode, decl.declarations[0]); 

I'm still looking for the fix, but at least this should help debug the issue.我仍在寻找修复程序,但至少这应该有助于调试问题。

You can do this using this .vscode/launch.json您可以使用此.vscode/launch.json

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug Ng Serve",
      "skipFiles": ["<node_internals>/**"],
      "cwd": "${workspaceFolder}",
      "program": "./node_modules/.bin/ng",
      "args": ["serve", "--verbose=true"]
    }
  ]
}

I had this error too.我也有这个错误。 I was missing to add all the packages I used in my code in the package.json depencencies (in my case ng-bootstrap ), after this the error vanished.我错过了在package.json (在我的情况下为 ng-bootstrap )中添加我在代码中使用的所有包,之后错误消失了。

I also spent a few minutes checking the issue and the simple ng lint was definitely not throw me to missing or failed imports, but when executing the ng lint with various flags like this:我还花了几分钟检查这个问题,简单的ng lint绝对不会让我丢失或失败导入,但是当执行带有各种标志的 ng lint 时:

ng lint --force=true --typeCheck=true

I found other things that helped me get to the error.我发现了其他帮助我解决错误的事情。

I had to go into each Page/Component and Module to check one by one.我必须将 go 放入每个页面/组件和模块中,以便一一检查。

Toggle all the VS Code folders and checking the "Problems" tab of the VS Code and thus right the imports.切换所有 VS Code 文件夹并检查 VS Code 的“问题”选项卡,从而正确导入。

This happened to me replicating one project to another and removing unused references and so on.这发生在我将一个项目复制到另一个项目并删除未使用的引用等等。

Since Angular didn't manage to provide a meaningful error description, let's ask TypeScript compiler for it.由于 Angular 没有设法提供有意义的错误描述,让我们询问 TypeScript 编译器。 In my case it worked like a charm, showing the exact place and reason:就我而言,它就像一个魅力,显示了确切的位置和原因:

tsc --project <path-to-your-project>/tsconfig.json

Pay attention, in case of several tsconfig's, use the one intended to build your application, it might be tsconfig.app.json whatever.请注意,如果有多个 tsconfig,请使用用于构建应用程序的那个,它可能是tsconfig.app.json什么的。 You can find it out under the options.tsConfig section of angular.json .您可以在angular.jsonoptions.tsConfig部分找到它。

It's all about wrong imports.这都是关于错误的进口。 You are trying to import the files that does not exist or their location is different:)您正在尝试导入不存在的文件或它们的位置不同:)

Run the build in dev mode.在开发模式下运行构建。 ng build and it points the error at the exact place. ng build并将错误指向确切的位置。

Not sure if you have already fixed this problem, but I've solved it by updating Angular (and it's dependencies) into 10.1.2不确定您是否已经解决了这个问题,但我已经通过将 Angular (及其依赖项)更新到 10.1.2 来解决它

Here's what I have actually.这就是我实际拥有的。

dependencies": {
"@angular/animations": "~10.1.2",
"@angular/cdk": "^10.2.2",
"@angular/common": "~10.1.2",
"@angular/compiler": "~10.1.2",
"@angular/core": "~10.1.2",
"@angular/forms": "~10.1.2",
"@angular/material": "^10.2.2",
"@angular/platform-browser": "~10.1.2",
"@angular/platform-browser-dynamic": "~10.1.2",
"@angular/router": "~10.1.2",
"rxjs": "~6.5.5",
"tslib": "^2.0.0",
"zone.js": "~0.10.  
},

"devDependencies": {
    "@angular-devkit/build-angular": "~0.1000.2",
    "@angular/cli": "~10.0.2",
    "@angular/compiler-cli": "~10.1.3",
    "@types/angular": "^1.7.3",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~3.3.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.9.5"
  }

I got this error every time a ran ng build or ng serve after a big merge.每次在大合并后运行ng buildng serve时,我都会收到此错误。 The only way that worked for me was by 'divide and conquer'.对我有用的唯一方法是“分而治之”。 I have moved to a new (test) branch and isolated/deleted all new modules/components/files that have come from merge and after every change I ran ng build .我已经移动到一个新的(测试)分支并隔离/删除了所有来自合并的新模块/组件/文件,并且在每次更改后我运行ng build When the error disappeared, I did undo previously deleted files and isolated smaller and smaller chunks until I got to the source of error.当错误消失时,我确实撤消了先前删除的文件并隔离了越来越小的块,直到找到错误的根源。 For me it was a wrong import path.对我来说,这是一个错误的导入路径。

I had the same error after updating from Angular 9 to 10.从 Angular 9 更新到 10 后,我遇到了同样的错误。

The problem was solved for me after updating dev dependencies:更新开发依赖项后,问题为我解决了:

npm update --save-dev 

In my Case -->在我的情况下-->

import { AppModule } from './app/app.module';  //main.ts file
import { ApicallService } from 'src/app/services/apicall.service'; //In a component file

These are are the reasons that showing me ERROR in Cannot read property '0' of undefined Error.这些是在无法读取未定义错误的属性“0”中向我显示错误的原因。 So I solved it with changing the paths.所以我通过改变路径来解决它。

import { AppModule } from './app/app/app.module'; //main.ts file
import { ApicallService } from 'src/app/app/services/apicall.service';

We had the same issue.我们有同样的问题。 After trying everything from this list, we updated all the dependencies and the message which was given was more descriptive.在尝试了此列表中的所有内容后,我们更新了所有依赖项,并且给出的消息更具描述性。 It was one import path that wasn't set correctly that caused the issue.这是导致问题的一个未正确设置的导入路径。

In my case it's very basic one.就我而言,这是非常基本的。 when I'm trying to "service" in my "component", i imported like当我试图在我的“组件”中“服务”时,我导入了

import  SetPasswordService  from './set-password.service';

But after checking each line, I added "curly braces" it's gone.但是在检查每一行之后,我添加了“花括号”,它就消失了。

import { SetPasswordService } from './set-password.service';

Virendra Singh Rathore is right Virendra Singh Rathore是对的

It's all about wrong imports.这都是关于错误的进口。 Your are trying to import the files that do not exist or their location is different:)您正在尝试导入不存在的文件或它们的位置不同:)

In my situation, the error appeared after transferring classes to another file.在我的情况下,将类转移到另一个文件后出现错误。 VS code didn't show the error location. VS 代码没有显示错误位置。 I had to open all the files and look for corrupted 'imports'.我必须打开所有文件并查找损坏的“导入”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM