2

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:

1 Answer 1

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.com/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 complete 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);
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

This sounds useful, but the problem (I simplified it in the question) is, that the elements of someArray are also Parse Objects with an array column of other Parse Objects. How can I include them as well? How can I "clear" all this fetched stuff, to make the response as small as possible? Whats wrong with filter?
Use dot notation, i.e. query.include('someArray.someOtherColumn'); As for .filter, it should be fine as long as you're not performing async operations inside.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.