Angular pobranie stringa z odpowiedzi na POST

0

Witam, chcę pobrać stringa "Error. Does this link come from email? Or maybe you have used your token already?" jako odpowiedź jeśli żądanie będzie niepoprawne (taką z resztą właśnie otrzymuje w Postmanie) lub "You have successfully changed your password." jeśli jest ok, a zamiast tego na swojej stronie otrzymuje Object object jeśli pobiorę bezpośrednio z parametru subscribe, a jak zastosuje funkcję JSON.stringify to otrzymuję coś takiego .

Oto mój kod:

  submitFunc() {
    this.data = '';
    if (this.uploadForm.invalid) {
      console.log('Password validation invalid')
      return;
    }
    console.log('Password validation correct')
    const response = this.restapiService.postResetPassword(this.tokenFromUrl, this.uploadForm.controls['password'].value);
    response.subscribe(data => { console.log('data '+JSON.stringify(data)); },
      error => { console.log('error '+JSON.stringify(error)); });
  }

oraz

  public postResetPassword(token: string, password: string): Observable<any> {
    const body = {
      'token': token,
      'password': password
    };
    const headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8');
    return this.http.post<any>('https://jakuwegiel-backend.herokuapp.com/reset_password', body,{headers: headers});
  }

oraz mój wycinek z kontrolera:

    @PostMapping(value = "/reset_password", consumes="application/json")
    public String processResetPassword(@RequestBody TokenAndPassword tokenAndPassword) {
        try {
            User user = userService.getByResetPasswordToken(tokenAndPassword.getToken());
            if (user == null) {
                return "message";
            } else {
                userService.updatePassword(user, tokenAndPassword.getPassword());
                System.out.println("You have successfully changed your password.");
            }
        }
        catch (Exception ex) {
            System.out.println("aaaaaaaaaaaaa " +ex.getMessage());
            return "Error. Does this link come from email? Or maybe you have used your token already?";
        }

        return "You have successfully changed your password.";
    }

Proszę o pomoc. Pozdrawiam

0

Dla błędów należało zastosować

console.log(error.error.text)

1 użytkowników online, w tym zalogowanych: 0, gości: 1