I encountered an issue while attempting to make a HTTP Post request. The error message I received is as follows:
auth.service.ts?c694:156 Something went wrong requesting a new password, error message: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
The first Http request preceding this one works perfectly fine.
This is the code snippet of my component calling the service:
requestNewPassword(formInput: NgForm) {
if(formInput.controls['email'].value != '')
this.authService.getApplicationAccessToken()
.mergeMap((response: IAuthAppResponse) => this.authService.requestNewPassword(formInput, response.access_token))
.subscribe(response => {
// Content removed for simplicity
})
}
Below is the service method generating the aforementioned error:
public requestNewPassword(formData: NgForm, applicationAccessToken: string): Observable<any> {
let headers = new HttpHeaders({
'Authorization':'Bearer ' + applicationAccessToken
});
let email: string = formData.controls['email'].value;
const body = {
email: email
};
console.log('requestNewPassword call header: ' + headers.get('Authorization'));
console.log('Email: ' + body.email);
return this.http.post(this.baseUrl + '/api/user/password/forgot', body, {headers}).do(response => {
console.log("New password was successfully sent to the e-mail adress");
}).catch((error: HttpErrorResponse) => {
console.log('Something went wrong requesting a new password, error message: ' + error.message);
return Observable.throw(error);
})
}
Whenever I submit an email through the form, triggering the requestNewPassword method in the component, I encounter the previously mentioned error from the service.
The logged header and email values are correct, so it seems that the data being provided is not the issue.
As I am unsure how to debug this further, I decided to seek assistance by posting this question here.
Thank you in advance!
Update
In an effort to pinpoint the problem, I have streamlined the code in my component by eliminating the chaining of the two HTTP requests and only executing the second one which is causing trouble.
requestNewPassword(formInput: NgForm) {
if(formInput.controls['email'].value != '')
this.authService.requestNewPassword(formInput, "")
.subscribe(response => {
// Content removed for simplicity
})
}
A complete stack trace is now displayed:
ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable...
at Object.subscribeToResult (subscribeToResult.js?c011:74)
...
at HTMLFormElement.globalZoneAwareCallback (zone.js?fad3:1566)
Since there is mention of html in the error message, I will also include the corresponding html code:
<form class="custom_form" #requestNewPasswordForm="ngForm" (ngSubmit)="requestNewPassword(requestNewPasswordForm)">
<label>E-mail</label>
<input class="custom_input" type="email" class="inputfield form-control" ngModel name="email">
<button class="btn btn-default" type="submit">
Send
</button>
</form>