Reputation: 25
I have a cloud code from which I call an external function. The cloud code response is null but the console displays the response
my cloud code ;
Parse.Cloud.define("testccadd", async request => {
try {
var ccaddrequest = {
conversationId: '123456789',
email: '[email protected]',
};
externalFunction (ccaddrequest, function (err, result) {
console.log(result);
return result;
}) ;
} catch (e) {
console.log("Error");
}
});
console.log (result);
shows the values from the external function, but the return result;
returns null
how can I get the external function response as response of my cloud code function ?
Upvotes: 0
Views: 127
Reputation: 199
The problem is that your externalFunction
uses a callback to return its result. That is an asynchronous event, meaning that it happens after your cloud functions has been processed.
The cloud function will execute var ccaddrequest...
and then call externalFunction
but it won't "wait" for externalFunction
to call the callback function if it contains asynchronous commands.
So you need to wrap the externalFunction
in a Promise (see how to promisify callbacks) and then await
the result of it.
Plus you need to return the result of the Promise, so in your code you need to add
Parse.Cloud.define("testccadd", async request => {
try {
var ccaddrequest = {
conversationId: '123456789',
email: '[email protected]',
};
var result = await externalFunctionPromise(...);
return result;
} catch (e) {
console.log("Error");
}
});
Upvotes: 1