0

I was using HttpClient with this code, and its working...

return this.http.post(this.env.API_URL+'/login', formData).pipe(
  tap(response => {
    let token = response['jwt'] ? response['jwt'] : null;
    this.storage.set('token', token)
    .then(
      () => { console.log('Token Stored: ' + token); },
      error => console.error('Error storing item', error)
    );
    this.token = token;
    this.isLoggedIn = true;
    return token;
  }),
);

Now I want to test HTTP native due to iOS issues, and I'm trying this code:

return this.http.post(this.env.API_URL+'/login', formData, {})
  .then(data => {
    console.log(data.status);
    console.log(data.data); // data received by server
    console.log(data.headers);
  })
  .catch(error => {
    console.log(error.status);
    console.log(error.error); // error message as string
    console.log(error.headers);
  }
);

However, I'm getting this error:

Property 'subscribe' does not exist on type 'Promise<void>'.

It's my first ever working with typescript and also Ionic... so, I have no clue on how to solve this...

Someone can help?

ADDITIONAL - this is how my form is calling this service

onSubmit(f: NgForm) {
  this.authService.login(f.value.usuario, f.value.senha).subscribe(
    data => {
      console.log('login ok');
    },
    error => {
      console.log(error);
    },
    () => {
      this.router.navigateByUrl('/tabs');
    }
  );
}
anderlaini
  • 1,593
  • 2
  • 24
  • 39
  • https://stackoverflow.com/questions/39319279/convert-promise-to-observable check this answer. – Yevgeniy.Chernobrivets Aug 02 '19 at 17:26
  • use `return this.http.post(this.env.API_URL+'/login', formData, {})` only in service. Remove `then()`. You are not allowed to use subscribe after using Promise. – varman Aug 02 '19 at 18:04

0 Answers0