2

i am having problem migrating from Http to HttpClient, this is the code that i use

constructor(public navCtrl: NavController, public http: HttpClient, private alertCtrl: AlertController) {

 this.http.get('http://example.com/date.php')
.subscribe(data => {
  this.posts = data;
  this.postsfilter = data.filter(item => item.date == todayDate);

 });

 }

i have included import 'rxjs/add/operator/filter'; but still getting error : Property 'filter' does not exist on type 'Object'. how to filter the return json file correctly?

0

2 Answers 2

2

By calling data.filter() you will access the Array.prototype.filter() method and not the RxJS's filter method. In your case, data contains an object which does not have a method filter. That's causing the error.

To use RxJS's filter you need to chain it to your Observable with the . notation.

That's how it looks like:

 this.http.get('http://example.com/date.php')
  .map(item => this.postsfilter = item)
  .filter(item => item.date === todayDate) // probably you would also want to go for strict equality
  .subscribe(data => {
    this.posts = data;
  });

Notice: Since RxJS 5.5 you are able to use the pipe method. THis basically changes the way you chain RxJS operators. Learn more about it here: https://blog.hackages.io/rxjs-5-5-piping-all-the-things-9d469d1b3f44

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for answering, i need both this.post and this.postsfilter. how can i use Array.prototype.filter(). because if i use method above i will have to call http.get twice. i want to filter it in ts file
@jx1986 you could use the map operator to assign the unfiltered value to this.postfilter. I've updated my answer. The way you use .filter() is already right. The problem is the returned data is not an array to loop through.
thanks Orlandster, would you like to elaborate how to map this, i'm newbie in this.
I've updated the code above. Make sure to add import 'rxjs/add/operator/map' in order to import the map operator.
this would help me a lot in updating my apps. i have marked it as answer but i couldn't upvote it as i'm new in this community. cheers n have a great day Orlandster !
0

"i have included import 'rxjs/add/operator/filter'; but still getting error : Property 'filter' does not exist on type 'Object'. how to filter the return json file correctly?" Because you don't say that data is an array

 this.http.get('http://example.com/date.php')
      .subscribe((data:any[]) => { //<--say that you received an array
         this.posts = data;
         //Here filter is filter of an ARRAY, not Rxjs
         this.postsfilter = data.filter(item => item.date == todayDate);
      });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.