0

I am using the following Parse.com Javascript query and need to switch the type of query based on a variable.

  function searchParseExercises (queryParam, ActiveFilters, searchParam) {
          var exercise = [];
          var TagSearch = Parse.Object.extend("Exercises");
          var query = new Parse.Query(TagSearch);
      query.containsAll("tags", ActiveFilters);

      query.limit(20);
      query.find({
        success: function(results) {
          var exerciseData = {};
          for (var i = 0; i < results.length; i++) {
            var object = results[i];
            var exerciseData = {
            exerciseName : object.get('exerciseName'),
            exerciseDescription : object.get('exerciseDescription'),
            images : object.get('images'),
          }
         exercise.push(exerciseData);
        }
         $scope.allExercises = exercise;
        },
        error: function(error) {
          $ionicPopup.alert({
            title: "Error: " + error.code + " " + error.message
            });
        }
      });
}

To clarify the requirement I have both a text search and filter search in my template. If there is a text search then it should perform:

query.contains("exerciseName", searchParam);

If there are ActiveFilters then it should perform this:

query.containsAll("tags", ActiveFilters);

However if both variables are present (searchParam and ActiveFilters) then it should perform a Compound Query.

I have no idea how I can wire all this up cleanly.

1 Answer 1

1

What I understood from your question:

if (ActiveFilters && searchParam) {
   Parse.Query.or(ActiveFilters, searchParam).find({
     success: function(results) {
         // results contains a list of players that either have won a lot of games or won only a few games.
     },
     error: function(error) {
         // There was an error.
     }
}
else if (ActiveFilters) {
    query.containsAll("tags", ActiveFilters);
}
else if (searchParam) {
    query.contains("exerciseName", searchParam);   
}
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry I should have been more clear. I need to have all eventualities 1. If text search only 2. Filters Only 3. Combination of both
It would be much better if you can edit your question and cite you requirement clearly. That way I would be able to help you in a much better way. Sorry, but again as per your last comment, I couldn't get the exact requirement.
See my updated answer, please feel free to ask any of your doubts.
The compound query does not work - I think that it needs the first part as per the docs?
Yes, it should be. The question was more related to changing scope variable. I haven't use parse ever so I can't tell you more on that.

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.