Reputation: 105
I am working on a payment system. I simulate the node.js using cloud functions. I send a request from Angular to Cloud Functions. Then, I send the second request from cloud functions to payment API. Payment API gets the request successfully and payment is successful. But, I guess because of an async structure, the response is shown as null on the Angular side.
Angular Code:
constructor(private fns: AngularFireFunctions) {
}
...
...
const callable = this.fns.httpsCallable('app');
var data = callable({req: request})
data.subscribe(async res => {
console.log(res);
});
Cloud functions:
exports.app = functions.https.onCall((data, context) => {
var res = "";
iyzipay.payment.create(data.req, function (err, result) {
res = result;
});
return res;
});
How can I wait for the response? I tried async/await but I don't know these subjects entirely. Thank you!
Upvotes: 2
Views: 140
Reputation: 105
After continuing the search, I found a solution like that:
Cloud functions:
exports.app = functions.https.onCall((data, context) => {
return new Promise((Resolve, Reject) => {
iyzipay.payment.create(data.req, async function (err, result) {
if (err) {
Reject({
id: 'Payment',
message: err
})
}
if (result && result.status == 'failure') {
Reject({
id: 'Payment Failed',
message: result.errorMessage
})
}
Resolve(result)
});
});
});
And angular side like Akif said:
const callable = this.fns.httpsCallable('app');
const res = await callable({ req: request }).toPromise();
console.log(res);
Upvotes: 3