4

In my angular app i am calling a service and fetching the data, i need to add a console log once the results are fetched. where can i add console log?

 this.searchTerm.asObservable()
            .debounceTime(300)      
            .distinctUntilChanged() 
            .switchMap(term => this.doFind(term))
            .subscribe(data => this.results = data
            , error => {
                this.errorMessage = error;
            });

3 Answers3

14

Use a code block

this.searchTerm.asObservable()
        .debounceTime(300)      
        .distinctUntilChanged() 
        .switchMap(term => this.doFind(term))
        .subscribe(data => {
           this.results = data;
           console.log('data', data);
        })
        , error => {
            this.errorMessage = error;
        });

or the do operator

 this.searchTerm.asObservable()
        .debounceTime(300)      
        .distinctUntilChanged() 
        .switchMap(term => this.doFind(term))
        .do(val => condole.log(val))
        .subscribe(data => this.results = data
        , error => {
            this.errorMessage = error;
        });
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
2

You are using the shorthand version of the arrow function, use the bigger version :)

.subscribe((data) => {
     this.results = data;
     console.log(data);
 }, error => {
    this.errorMessage = error;
 });
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
1

u can do like this

  this.searchTerm.asObservable()
        .debounceTime(300)      
        .distinctUntilChanged() 
        .switchMap(term => this.doFind(term))
        .subscribe(
            data=>this.message=data.msg,
            error=>alert('errer'),
            ()=>{console.log('finished');}

            );
Asad
  • 3,070
  • 7
  • 23
  • 61