Im new to angular and im trying to get value from method by subscribing. here's the method I'm trying to get value from. I'm not quiet sure how this method works but I know that its supposed to return {ClientId, ClientSecret} Object
clientSecret(merchantId: number): Observable<void> {
let url_ = this.baseUrl "/api/PosQR/{merchantId}/client-secret";
if (merchantId === undefined || merchantId === null)
throw new Error("The parameter 'merchantId' must be defined.");
url_ = url_.replace("{merchantId}", encodeURIComponent("" merchantId));
url_ = url_.replace(/[?&]$/, "");
let options_ : any = {
observe: "response",
responseType: "blob",
headers: new HttpHeaders({
})
};
return this.http.request("get", url_, options_).pipe(_observableMergeMap((response_ : any)
=> {
return this.processClientSecret(response_);
})).pipe(_observableCatch((response_: any) => {
if (response_ instanceof HttpResponseBase) {
try {
return this.processClientSecret(response_ as any);
} catch (e) {
return _observableThrow(e) as any as Observable<void>;
}
} else
return _observableThrow(response_) as any as Observable<void>;
}));
}
Im trying to subscribe to it in ngOnInit and save the value in local variable
this.posQrClient.clientSecret(2343).subscribe(res => {
this.clientId = res[ClientId]
});
it returns null. I've tried to solve it lots of ways but couldn't figure it out. If anyone can help me I will greatly appreciate it
CodePudding user response:
Your subscription looks correct, that is one way to grab values from an observable but in your case you are grabbing void values.
An Observable<void>
is a stream of void
values. It sounds like you want a stream of {ClientId, ClientSecret}
values. In that case you should type your function like this,
clientSecret(merchantId: number): Observable<{clientSecret: string; clientId: string}> {...}
Then you will need to make sure that you are correctly returning that type. If you're comfortable using any you could also do,
clientSecret(merchantId: number): Observable<any> {...}
But I don't recommend that.