Angular,TypeError:无法读取null的属性“任务”

时间:2018-06-27 10:18:05

标签: angular

我在Angular上写了项目,我尝试显示文章,但有这样的错误,也无法创建文章。浏览器控制台中显示以下错误:

TypeError: Cannot read property 'tasks' of null
at task.ts:30
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:388)
at Object.onInvoke (core.js:3654)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:387)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (zone.js:138)
at zone.js:872
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3645)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)

ERROR Error: Uncaught (in promise): Cannot read property 'tasks' of null
at resolvePromise (zone.js:814)
at resolvePromise (zone.js:771)
at zone.js:873
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:3645)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
at drainMicroTaskQueue (zone.js:595)
at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:500)
at invokeTask (zone.js:1540)

最有可能导致错误的代码:

 26  loadTasks(): Promise<Task[]> {
 27     const url = `${this.tasksUrl}?access_token=${localStorage.getItem('token')}`;
 28     return this.http.get(url)
 29      .toPromise()
 30      .then(res => res.json().tasks as Task[])
 31      .catch(error => this.handleError(error, 'Could not load tasks!'));
 32  }
 33  getTask(id: number): Promise<Task> {
 34      const url = `${this.tasksUrl}/${id}?access_token=${localStorage.getItem('token')}`;
 35      return this.http.get(url)
 36      .toPromise()
 37      .then(res => res.json() as Task)
 38      .catch(error => this.handleError(error, 'Could not load task!'));
 39  }
 40  create(task): Promise<Task | void> {
 41    task['due_date'] = task['due_date']['formatted'];
 42    let body = JSON.stringify({task: task});
 43    const url = `${this.tasksUrl}?access_token=${localStorage.getItem('token')}`;
 44    return this.http.post(url, body, { headers: this.headers })
 45      .toPromise()
 46      .then(res => res.json() as Task)
 47      .catch(error => {
 48        this.handleError(error, 'Could not create task!')
 49      });
 50  }

0 个答案:

没有答案