0

I am building an AngularJS CRUD site to add data to Parse.com to be used by mobile apps that will read it. (The CRUD is easier to do on a real keyboard due to the amount of data added.) First step is to create and save the Child objects (Ingredients and Steps), and then to create and save a Recipe object that has a Relation to the Ingredients and Steps. I need to use Relation and not Pointers for these. (because I may need this to be many to many in future)

Here's my problem: writing the query to find any Ingredients and Steps that were created that are NOT yet part of a relation, to find the ones to be added to a new recipe.

The examples in the Javascript SDK don't show this scenario, they only show a query for a relation that exists, but does not have an additional attribute on the related item (comments for posts where post doesn't have an image).

Recipe has ingredients, a Relation<Ingredient>, and steps, a Relation<Step>.

This doesn't work to get the Ingredients that are not yet related to a Recipe.

    var Recipe = Parse.Object.extend("Recipe");
    var Ingr = Parse.Object.extend("Ingredient");

    recipeQuery= new Parse.Query(Recipe);
    recipeQuery.exists("ingredients");

    query = new Parse.Query(Ingr);
    query.doesNotMatchQuery("recipe",recipeQuery);
    query.ascending('name');
    query.find({
        success: function (data) {
            if (data.length > 0) {
                $scope.loadingMsg = '';
            }
            $scope.ingredients = data;
        }
    });
Jennifer S
  • 1,419
  • 1
  • 24
  • 43
  • I think this can't be done with single query, if you want with multiple queries i can try :) – Nikolay Rusev Jul 23 '15 at 13:04
  • I just need a solution that will work, and to understand what it is doing, so I can get my CRUD to work. If it takes multiple queries, that's fine. I would appreciate any help you can provide. Thanks! – Jennifer S Jul 23 '15 at 13:21

1 Answers1

1
angular
.module('app.services')
.service('siteService', function($q, ParseService, $timeout) {
    var that = this;
    this.fetchIngr = function() {
        return this.fetchRecipes().then(function(recipes) {
            var q = recipes.reduce(function(initial, current) {
                initial.push(that.fetchIngredient(current));
                return initial;
            }, []);
            return $q.all(q);
        });
    };

    this.fetchRecipes = function() {
        var defer = $q.defer();
        var Recipe = Parse.Object.extend("Recipe");
        recipeQuery = new Parse.Query(Recipe);
        recipeQuery.ascending('name');
        recipeQuery.find({
            success: function(data) {
                console.log('data', data)
                defer.resolve(data);
            },
            error: function(error) {
                defer.reject(error);
            }
        });
        return defer.promise;

    };
    this.fetchIngredient = function(recipe) {
        var defer = $q.defer();
        recipe.relation('ingredients').query().find({
            success: function(res) {
                defer.resolve(res);
            },
            error: function(error) {
                defer.reject(error);
            }
        });
        return defer.promise;
    };

})

Usage:

 siteService.fetchIngr().then(function(all){
            console.log('flatten all ingr for used recipes',[].concat.apply([], all));
        });

in the fetchIngr function you will receive array of all ingredients that are used in the Recipe. then you have to the diff between two arrays to find not used ingredients. To do the subtract you should fetch all ingredients and to make subtraction between two arrays (one used ingrdients for recipes and the one with all ingredients). see here the post : JavaScript array difference

if you have questions feel free to ask me.

Community
  • 1
  • 1
Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29
  • Thanks so much! I'm going to try this out in the morning. Today I have been looking at other ways to select a subset for the many to many option, but your solution may get me the right set for 99% of the time. – Jennifer S Jul 23 '15 at 21:10
  • what happened?is everytging ok? – Nikolay Rusev Jul 26 '15 at 17:22
  • Is this example meant to be cloud code, or just added as a separate js file? (I didn't have a chance to get this in yet (due to other work obligations) – Jennifer S Jul 27 '15 at 17:58