Search code examples
javascriptjsonparse-platformparse-cloud-code

Parse cloud code, access object relations


I'm trying to create a Parse cloud code function that returns the same result as a GET on parse/classes/MyClass but with the IDs of the relations.

I've done it for one object, but I can't make it work in a loop to get all the objects.

This is how I'm trying to get all the objects. It's working without the for loop and with r as a response.

Parse.Cloud.define('get_ClassName', function(request, response) {
  let query = new Parse.Query('ClassName');
  var ret = {};

  query.find({useMasterKey: true}).then(function(results) {
        for (var i = 0; i < results.length; i++) {
                ret[i] = {};
                const relQuery = results[i].get('status').query();
                relQuery.find({useMasterKey: true}).then(function(res) {
                        var ids = {};
                        for (var j  = 0; j < res.length; j++) {
                                ids[j] = res[j].id;
                        }
                        var status = {...status, id: ids};
                        status["className"] = "Status";
                        var r = {...r, status: status};
                        r["tag"] = results[i].get("tag");
                        ret[i] = r; //Can't access ret
                        //response.success(r); //Working
                })
        }
        response.success(ret);
  });
});

This is the actual result for the working version:

{
    "result": {
        "status": {
            "id": {
                "0": "xxxxxx",
                "1": "xxxxxx"
            },
            "className": "Status"
        },
        "tag": "value"
    }
}

Solution

  • response.success(ret); will run before relQuery.find finish in for loop.

    Use Promise.all()

    or Async await and refactor your logic.

    I comment on your code about your missing.

    Parse.Cloud.define('get_ClassName', function(request, response) {
      let query = new Parse.Query('ClassName');
      var ret = {};
    
      query.find({useMasterKey: true}).then(function(results) { // Asyncronous
            for (var i = 0; i < results.length; i++) {
                    ret[i] = {};
                    const relQuery = results[i].get('status').query();
                    relQuery.find({useMasterKey: true}).then(function(res) { // Asyncronous
                            var ids = {};
                            for (var j  = 0; j < res.length; j++) {
                                    ids[j] = res[j].id;
                            }
                            var status = {...status, id: ids};
                            status["className"] = "Status";
                            var r = {...r, status: status};
                            r["tag"] = results[i].get("tag");
                            ret[i] = r; //Can't access ret
                            //response.success(r); //Working
                            console.log(`index {i}`, r);
                    })
            }
            console.log(`response will be called`);
            response.success(ret); // Called before `relQuery.find` finish
      });
    });