Angular2, How to set json data in another function

36 views
Skip to first unread message

sri devi

unread,
May 30, 2016, 7:47:27 AM5/30/16
to AngularJS
I need to set json data from one to another function.
But i can't. Is there any mistaken in my code.
 ngOnInit() {
    this.getNames();
   }
    
 
 getNames() {
    this.http.get('app/mock/names.json')
      .map((res:Response) => res.json())
      .subscribe(
         data => this.filter(data),
         err => console.error(err),
         () => console.log('done')
          
      );
      
   }
      
   
  filter(data) {
      if (this.query !== ""){
            console.log(data);
       }
    } 

I got only undefined message. Anyone plz help me


Sander Elias

unread,
May 31, 2016, 2:04:55 AM5/31/16
to AngularJS
Hi Sri,

First, you are adding null code to your app. 
.subscribe(
   data
=> this.filter(data),
   err
=> console.error(err),
   
() => console.log('done')
 
);

is exactly the same as:
.subscribe(
   
this.filter,
   err
=> console.error("Oops, an err:",err),

   
() => console.log('done')
 
);

secondly, I think your code throws a empty error, so you get an undefined. Put some text in your log's so you know where the console outputs are originating..
Then log the data in the filter function outside of the if statement first, so you know where to look for trouble.

Hope this helps,
Regards
Sander


Reply all
Reply to author
Forward
0 new messages