How to make a console.log on the map?

0

Notice the algorithm in Angular

findToOptions(searchValue: string): Observable<any[]> {
    return this.findAll(new PageParameters(15, 0, 'id,asc'), this.getSearch(searchValue),
      this.fields.toString(), true)
      .pipe(
        map(page => page.content)
      );
  }

Please, how do I put a console.log in this method?

    
asked by anonymous 11.12.2018 / 17:33

2 answers

1

You need to expand your arrow function in addition to just one expression so you can add others:

findToOptions(searchValue: string): Observable<any[]> {
    return this.findAll(new PageParameters(15, 0, 'id,asc'), this.getSearch(searchValue),
      this.fields.toString(), true)
      .pipe(
        map(page => {
            console.log('Sua mensagem aqui');
            return page.content;
        })
      );
  }
    
11.12.2018 / 17:40
0

Use {} in your arrow function to define the scope and place the console.log in it.

Example

{ 
    content.map( content => {
        console.log(content);
        return content.id
    })
}

According to documentation .

  

The arrow function can have a "concise scope" or "block scope"   usual.

     

In a concise scope, only one expression is required and one return   implicit is attached.

     

In a block scope, you must use a   explicit return statement.

Example:

var func = x => x * x;    

Concise, so you do not need a% implicit%

var func = (x, y) => { return x + y; }; 

This block needs an implicit%% as it is not concise.

In your example you can just add return and put a return before

findToOptions(searchValue: string): Observable<any[]> {
return this.findAll(new PageParameters(15, 0, 'id,asc'), this.getSearch(searchValue),
  this.fields.toString(), true)
  .pipe(
    map(page => {
        console.log(page.content);
        return page.content;
    })
  );

}

    
11.12.2018 / 17:44