How to make a console.log on the map?

Asked

Viewed 105 times

0

Note 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 place a.log console in this method?

2 answers

1


You need to expand your Arrow Function apart from only one expression in such a way that it is possible to add other:

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;
        })
      );
  }

0

Utilize {} in his arrow function to set the scope and put the console.log inside it.

Example

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

According to the documentation.

The Arrow Function may have a "concise scope" or "block scope" usual.

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

In a block scope, you should use a explicit return instruction.

Example:

var func = x => x * x;    

Concise, so you don’t need a return implicit

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

This block needs a return implicit because it is not concise.


In your example you can just add {} and put a console.log 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;
    })
  );

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.