2

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!

1 Answer 1

3

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); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.