How to apply console.log at the angle?

Asked

Viewed 4,161 times

0

I want to apply console.log in the angular method, but I’m having difficulty, I’m doing so;

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’re making this mistake;

ERROR in src/app/cevejas/cerveja.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 he’s here he works perfectly, and he’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 just wish I knew how to apply the console.log in the method.

  • Your error has nothing to do with the console, read your error message well Type 'Promise' is not assignable to type 'Promise'. and check if the method as it is calling the http.post() is correct. Depending on the version and the module you are using the call is different.

  • is correct yes, could you please take a look at my post as I just updated.

  • 2

    I could try like this: then(response => {&#xA; console.log(response.json()); return response.json();&#xA; }); I found that Uma arrow function vazia retorna undefined maybe that’s the problem. But I’m not sure.

  • @Everson calm I’ll try

  • @Everson took it, that’s the way I wanted it, thank you very much, post your reply to me score as sure.

1 answer

2


Your return is incorrect.

See that you should be returning a Promise of the kind Cerveja, but you’re returning the 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 excerpt then, and call it in its component

  • No @André, I don’t want to remove the then, if you watch carefully my post the add method is working perfectly, my project is managing to save my beer entity, what exactly I want to do is to know how to apply the console.log in the method so I can see the data entered in the browser console, did you understand now? if you don’t understand I can try to explain in a different way.

  • I get it, @wladyband. But it seems wrong to subscribe to the service itself, because you are missing the benefits of Observables/Promises. With the proposed solution, you will see that your IDE will do the intelissense incorrectly, as you will have an object of type Cerveja, and not a Promise of the kind Cerveja. The proper way to do this would be by using the method map and still do the then in the component

  • I updated the example using the operator map

Browser other questions tagged

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