admin管理员组

文章数量:1426620

I am working on a parse cloud code function which performs a query and filters the results afterwards. These are my first lines of code written in JavaScript, so I have no clue how to solve the following problem.

The problem is, that my filter predicate needs some elements stored in the someArray variable. All elements of someArray aren't fetched yet. But fetching an an Parse.Object is a asynchronous call, so I have no chance to return trueor false expected by filter().

How can I solve this problem?

Or I am misinterpreting the fact, that when I don't fetch the arrayElement

console.log("arrayElement with name: ".concat(typeof(arrayElement), arrayElement.get("name")));

prints

arrayElement with name:objectundefiend

although I know that Object represented by arrayElment has a name-column which is always defined?

//The cloud code
Parse.Cloud.define("search", function(request, response) {
    var query = new Parse.Query("Location");
    query.withinKilometers("gps", request.params.searchLocation, request.params.searchRadius / 1000);
    query.find({
        success: function(results) {
            // results is an array of Parse.Object.
            var locations = results.filter(function(location) {
                console.log("Location with name: ".concat(location.get("name")));
                var someArray = location.get("someArray");
                if (someArray instanceof Array) {
                    console.log("The array of this location has ".concat(someArray.length, " elements."));
                    someArray.forEach(function(arrayElement) {
                        arrayElement.fetch().then(
                            function(fetchedArrayElement) {
                                // the object was fetched successfully.
                                console.log("arrayElement with name: ".concat(typeof(fetchedArrayElement), fetchedArrayElement.get("name")));
                                if (menuItem) {};
                                return true;
                            }, 
                            function(error) {
                                // the fetch failed.
                                console.log("fetch failed");
                            });
                    });
                }
            });
            response.success(locations);
        },
        error: function(error) {
            // error is an instance of Parse.Error.
            response.error(error);
        }
    });
});

some useful links:

  • Parse JavaScript SDK API
  • Parse Cloud Code Guide

I am working on a parse cloud code function which performs a query and filters the results afterwards. These are my first lines of code written in JavaScript, so I have no clue how to solve the following problem.

The problem is, that my filter predicate needs some elements stored in the someArray variable. All elements of someArray aren't fetched yet. But fetching an an Parse.Object is a asynchronous call, so I have no chance to return trueor false expected by filter().

How can I solve this problem?

Or I am misinterpreting the fact, that when I don't fetch the arrayElement

console.log("arrayElement with name: ".concat(typeof(arrayElement), arrayElement.get("name")));

prints

arrayElement with name:objectundefiend

although I know that Object represented by arrayElment has a name-column which is always defined?

//The cloud code
Parse.Cloud.define("search", function(request, response) {
    var query = new Parse.Query("Location");
    query.withinKilometers("gps", request.params.searchLocation, request.params.searchRadius / 1000);
    query.find({
        success: function(results) {
            // results is an array of Parse.Object.
            var locations = results.filter(function(location) {
                console.log("Location with name: ".concat(location.get("name")));
                var someArray = location.get("someArray");
                if (someArray instanceof Array) {
                    console.log("The array of this location has ".concat(someArray.length, " elements."));
                    someArray.forEach(function(arrayElement) {
                        arrayElement.fetch().then(
                            function(fetchedArrayElement) {
                                // the object was fetched successfully.
                                console.log("arrayElement with name: ".concat(typeof(fetchedArrayElement), fetchedArrayElement.get("name")));
                                if (menuItem) {};
                                return true;
                            }, 
                            function(error) {
                                // the fetch failed.
                                console.log("fetch failed");
                            });
                    });
                }
            });
            response.success(locations);
        },
        error: function(error) {
            // error is an instance of Parse.Error.
            response.error(error);
        }
    });
});

some useful links:

  • Parse JavaScript SDK API
  • Parse Cloud Code Guide
Share Improve this question asked Apr 19, 2014 at 22:37 Robin des BoisRobin des Bois 3552 gold badges6 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

If I'm reading this right, you've got an array column of Parse Objects. You should use the include method on your query so that the objects are fetched in the initial query.

query.include('someArray');

I don't think you'll want to use a filter, and should take a look at the Promises documentation here: https://parse./docs/js_guide#promises

You're right that since it's asynchronous, you need to wait for everything to be done before calling response.success which doesn't currently happen in the code. Promises and defining your own async functions that use Promises is a great way of chaining functionality and waiting for groups of functions to plete before going forward.

If all you wanted to do what include that column, the whole thing should just be:

Parse.Cloud.define("search", function(request, response) {
  var query = new Parse.Query("Location");
  query.include('someArray');
  query.withinKilometers("gps", request.params.searchLocation, request.params.searchRadius / 1000);
  query.find().then(function(results) {
    response.success(locations);
  }, function(error) {
    response.error(error);
  });
});

本文标签: javascriptHow to fetch an object in Parse Cloud CodeStack Overflow