user21597761
user21597761

Reputation:

Angular get string from POST response

I am trying to get string "Error. Does this link come from email? Or maybe you have used your token already?" as a response if requesty is not correct (excactly such response I get in Postman) or "You have successfully changed your password." if request is ok, but instead of that on my website when I get directly from param of subscribe I get Object object, but when I use function JSON.stringify then I get something like that.

Here is my code:

  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('a '+JSON.stringify(data)); },
      error => { console.log('b '+JSON.stringify(error)); });
  }

and

  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});
  }

and my part of controller in backend

@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.";
}

Is there anything you need more?

Upvotes: 0

Views: 705

Answers (1)

Emilien
Emilien

Reputation: 2761

You need to console.log(error.error.text). And then you will be able to make this value in your text message.

Upvotes: 1

Related Questions