0

I'm trying to print tasks when I use res.json(), I get nothing when I use res.tasks. I get another error, only new ones are shown, old ones are not displayed, and if the page is reloaded, then new ones will also be removed.

task.ts


....

   loadTasks(): Promise<Task[]> {
   const url = `${this.tasksUrl}?access_token=${localStorage.getItem('token')}`;
   return this.http.get(url)
     .toPromise()
     .then(res => res.tasks as Task[]) // res.json()
     .catch(error => this.handleError(error, 'Could not load tasks!'));
}

....

Error terminal:

ERROR in src/app/services/task.ts(30,24): error TS2339: Property 'tasks' does not exist on type 'Response'.

Error console browser:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'constructor' of undefined
TypeError: Cannot read property 'constructor' of undefined
at FilterByFieldPipe.push../src/app/pipes/filter-by-field.ts.FilterByFieldPipe.transform (filter-by-field.ts:10)

and filter-by-field.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'filterByField',
  pure: false
})

export class FilterByFieldPipe implements PipeTransform {
  transform(items, key, value) {
    if (items.constructor === Array) {
      return items.filter((item) => item[key] === value);
    }
  }
}
jps
  • 11,454
  • 12
  • 42
  • 55
andrea2010
  • 342
  • 2
  • 13
  • Are you using the old http or the new httpClient? – David Jul 04 '18 at 09:32
  • I'm using a new httpClient, – andrea2010 Jul 04 '18 at 09:36
  • 1
    Try using this `.then((res:any) => res.tasks as Task[])` for the terminal error. For the other one, you need to show how you call your pipe. You're probably calling it on an undefined variable – David Jul 04 '18 at 09:40
  • https://stackoverflow.com/questions/47202177/angular-4-http-to-httpclient-property-someproperty-does-not-exist-on-type/47202304 – AJT82 Jul 04 '18 at 09:46
  • And thank you, the advice with an error in the console helped. – andrea2010 Jul 04 '18 at 09:55
  • I noticed that if I create two different accounts, create tasks from them, then tasks are not assigned to one user, how can this be fixed? – andrea2010 Jul 04 '18 at 10:11

0 Answers0