1

I have been trying to migrate my Cloud functions from Parse Server 2.8.4 to 3.0+.

This following Cloud function aims to return count of GCUR_OBSERVATION to the client. I intended to use native promises in synchronous instead of async/await.

Parse.Cloud.define("countOfObservations", (request) => {
    var query = new Parse.Query("GCUR_OBSERVATION");
    var countOfObs = 0;
    query.count({ useMasterKey: true }).then( (count) => {
        countOfObs = count;
        console.log("*** count=" + countOfObs);
        return countOfObs;
    });
});

When I tried call this function from cURL:

curl -X POST -H "X-Parse-Application-Id: {APP_ID}" -H "X-Parse-REST-API-Key: {REST_API_KEY}t" -H "Content-Type: application/json" https://{SERVER_URL}/parse/functions/countOfObservations

{} was returned. However the back-end console printed *** count=2882.

Is there anything I did wrong?

alextc
  • 3,206
  • 10
  • 63
  • 107

2 Answers2

0

You are not returning the promise itself. You can use async/await to make it more simple.

Here is an updated example.

Parse.Cloud.define("countOfObservations", (request) => {
    var query = new Parse.Query("GCUR_OBSERVATION");
    var countOfObs = 0;
    return query.count({ useMasterKey: true }).then( (count) => {
        countOfObs = count;
        console.log("*** count=" + countOfObs);
        return countOfObs;
    });
});
ssakash
  • 640
  • 8
  • 23
0

try this:

Parse.Cloud.define("countOfObservations", async (request) => {
    var query = new Parse.Query("GCUR_OBSERVATION");
    var countOfObs = 0;
    const count = await query.count({ useMasterKey: true });
    countOfObs = count;
    console.log("*** count=" + countOfObs);
    return countOfObs;
});
uzaysan
  • 583
  • 1
  • 4
  • 18