0

希望对此有所帮助。我一直在尝试许多不同的方法,只是发现这种方法非常复杂,并且认为它可能更简单,因为我还没有一个可行的解决方案。我已询问 Parse.com 社区,但尚未得到回复。

我的数据存储(parse.com)有两个类/表。食物类型和食物项目。Food Type 和 Food Items 之间存在一对多的关系。例如,Produce 是类 Food Type 中的(对象“名称”)的一个属性,它有许多与之相关的食物项目(如苹果、香蕉、生菜等)。这已在数据存储中正确构建。

在数据存储中,Food Type 具有三个对象 - colour、foodItemId(relation)和 name。Name 是指食物类型的名称,例如Produce,Color 只是我给name 和foodItemId 分配的颜色,点击关系时,我现在添加了一堆水果进行测试。

Food Item Class 有四个对象/元素 - expiry、keyworkType(指的是水果、蔬菜还是其他)、名称和照片。这些都是不言自明的。

我想要做的是当用户想要将一个项目添加到他们的库存中时,他们选择食物类型(例如农产品)>它将他们带到一个页面,该页面将打印出该食物类型的所有食物项目(例如查询将检索属于特定食物类型的所有食物)。

所以我实际上想要做的是匹配一种食物类型 - “名称”与用户输入的那个(在这种情况下是生产)>然后检索属于该食物类型的所有食物项目。因此,我想将所有“子”食品对象检索到食品类型>然后能够使用返回的食品来操纵和玩弄我想要的任何东西。

简而言之,我尝试过的一切都不起作用,我希望得到一些帮助。

多谢你们,

基尔

4

1 回答 1

0

检查一下...我以为我找到了答案,结果证明是错误的。我已经找到了答案(这是你需要的代码 - 这个参考有帮助!!!https://www.parse.com/questions/can-i-use-include-in-a-query-to-include -all-members-of-a-parserelation-error-102以及https://parse.com/docs/js/symbols/Parse.Relation.html):

//create an array that will hold all the unique KeywordsTypes in it.
// This will be sent to another method to print out items that are in a KeywordType.
var keyword_type_array = new Array();

//get the info out of the db and then set it in the variables
var FoodType = Parse.Object.extend("Food_Type");
var FoodItem = Parse.Object.extend("Food_Item");    

//we want to find the Food Items that belong to a Food Type.
var query = new Parse.Query(FoodType);  

// so make it so the inner query actually says find the matching food type
query.equalTo("name", food_type);
query.first({
    success: function(results) {
        // get the results (object) and put them in a variable which will be used with relation
        var innerQuery = results;
        //make it a relational query.. aka.. find for the Food Type returned, the foodItemId and all that are in that corrospond to that field.
        var relation = innerQuery.relation("foodItemId"); //https://www.parse.com/questions/can-i-use-include-in-a-query-to-include-all-members-of-a-parserelation-error-102
        //run the relational query.
        relation.query().find({
            success: function(theFoodItems) {
                for (var i = 0; i < theFoodItems.length; i++) {             
                    //add all keywordTypes to the keyword type array. We will go through and get unique types out.
                    keyword_type_array[i] = theFoodItems[i].get("keywordType");                             
                }

                // create a new array that will hold all unique keyword types.
                var unique_keywords = new Array();

                // get unique keywords types.
                for (var i = 0; i < keyword_type_array.length; i++) {
                    var unique = true;

                    for (var j = 0; j < unique_keywords.length; j++) {
                        if (keyword_type_array[i] == unique_keywords[j]) {
                            unique = false;
                            break;
                        }                   
                    }

                    if (unique) {
                        // if a keyword is found to be unique.. put that keyword in the array.
                        unique_keywords.push(keyword_type_array[i]);                    
                    }
                }

                // So now we know what we need to print, lets call a function to print them.
                print_view_sections(unique_keywords);

            },
            error: function(object, error) {
                // The object was not retrieved successfully.
                // error is a Parse.Error with an error code and description.
                alert("Error: " + error.code + " " + error.message)
            }
        });
    },
    error: function(object, error) {
        // The object was not retrieved successfully.
        // error is a Parse.Error with an error code and description.
        alert("Error: " + error.code + " " + error.message)
    }
});
于 2013-06-05T12:53:12.737 回答