Hi!
I have a Meteor based backend and native iOS app. I’m using restivus
as API endpoint and in-app-purchase
libs for validating receipt.
The problem is I can’t figure out how to get response from Meteor.methods back asynchronous.
Here is my code.
method.js
Meteor.methods({
'validateReceipt': function(appleReceipt) {
var iap = require('in-app-purchase');
var receipt = [];
iap.config({
applePassword: 'my_super_secret_password'
});
iap.setup()
.then(() => {
iap.validate(appleReceipt).then(onSuccess).catch(onError);
})
.catch((error) => {
console.log('Error: ' + error);
});
function onSuccess(response, validatedData) {
var options = {
ignoreCanceled: true,
ignoreExpired: true
};
console.log('RECEIPT: ' + JSON.stringify(response.receipt, null, 2));
return response.receipt; // === here what I want to call back
}
function onError(error) {
console.log('ERROR: ' + error);
return error;
}
}
});
Here is how I call validateReceipt
method in restAPI.js
. When I call this endpoint thru POST request to http://../validateReceipt
address it executes function defined in action:
section
var Api = new Restivus({
prettyJson: true,
useDefaultAuth: true
});
// Validate receipt
Api.addRoute('validateReceipt', {authRequired: false}, {
post: {
authRequired: false,
action: function() {
var receipt = this.bodyParams.receiptData;
var data = [];
Meteor.call('validateReceipt', receipt, function(error, result) {
if (error) {
console.log('Error: ' + error);
} else {
console.log('Result: ' + result);
data = result;
}
console.log('============= RESULT: ', data); //=== data is `undefined`
});
return {status: 'success', data: data};
}
}
});
I receive undefined
because of function return immediately and doesn’t wait while method return. But I can’t understand how to rewrite this async to get result.
Please help.
Thanks.