I am utilizing HttpClient for sending requests. I have created a service to handle this.
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders, HttpResponse ,} from '@angular/common/http';
import {Observable} from 'rxjs';
import {map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class HttpService {
apiRoot: string = 'https://xxxx/api';
constructor(private http: HttpClient) { }
getQueryParam(obj) {
let search = new HttpParams();
for (let key in obj) {
search = search.set(key, obj[key]);
}
return search;
}
post(apiURL, params, data){
console.log('Inside Post Request');
let paramsData = this.getQueryParam(params);
let CompleteURL = `${this.apiRoot}` + apiURL;
return this.http.post(CompleteURL,data,{params : paramsData,observe : 'events'});
}
}
In my component:
login() {
this.admin.password = Md5.hashStr(<string>this.admin.password);
this._httpService.post('/loginAdmin', { "name": "vinay" }, this.admin)
.subscribe(res => {
console.log(res.success);
},
(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
console.error('An error occurred:', err.error.message);
} else {
console.error(`Backend returned code ${err.status}, body was: ${err.error}`);
}
}
);
}
When trying to access the response body value in my component, an error is shown stating that "Property 'success' does not exist on type 'HttpEvent<Object>
'". I have researched online and found suggestions about creating an interface, but the issue persists as "res.success" can be either an array or object. Any assistance would be greatly appreciated.