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 thehttp.post()
is correct. Depending on the version and the module you are using the call is different.– celsomtrindade
is correct yes, could you please take a look at my post as I just updated.
– wladyband
I could try like this:
then(response => {
 console.log(response.json()); return response.json();
 });
I found thatUma arrow function vazia retorna undefined
maybe that’s the problem. But I’m not sure.– Don't Panic
@Everson calm I’ll try
– wladyband
@Everson took it, that’s the way I wanted it, thank you very much, post your reply to me score as sure.
– wladyband