How to apply console.log in angular?

-1

I want to apply console.log in the angular method, but I'm having trouble, I'm doing this;

export class CervejaService {

    cervejasUrl = 'http://localhost:8080/cervejas';

    constructor(private http: Http) { }

    adicionar(cerveja: Cerveja): Promise<Cerveja> {
        const headers = new Headers();
        headers.append('Content-Type', 'application/json');
        return this.http.post(this.cervejasUrl,
            JSON.stringify(cerveja), { headers })
            .toPromise()
            .then(response => {
                console.log(response.json());
            });
    }
}

But you are generating this error;

  

ERROR in src / app / cevejas / cerveza.service.ts (15,9): error TS2322: Type   'Promise' is not assignable to type 'Promise'. Type   'void' is not assignable to type 'Beer'.

How do I apply the console.log ?

The way it's here it works perfectly, and it's saving

adicionar(cerveja: Cerveja): Promise<Cerveja> {
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
        return this.http.post(this.cervejasUrl,
        JSON.stringify(cerveja),  { headers })
      .toPromise()
      .then(response =>  response.json());
  }

I would just like to know how to apply the console.log in the method.

    
asked by anonymous 22.01.2018 / 18:10

1 answer

1

Your return is incorrect.

See that you should be returning Promise of type Cerveja , but you're returning type void

adicionar(cerveja: Cerveja): Promise<Cerveja> {
        const headers = new Headers();
        headers.append('Content-Type', 'application/json');
        return this.http.post(this.cervejasUrl,
            JSON.stringify(cerveja), { headers })
            .toPromise()
            .map(cerveja => {
              console.log(cerveja);
              return cerveja;
            })
    }

You probably want to remove the then snippet, and call it in your component

    
22.01.2018 / 18:57