0

客户提交新的recipe. 该recipe对象包括一个数组,cuisines该数组类似于此配方所属的类别。

beforeSave()Cloud Code 函数中,我想在具有recipes 和cuisines 的单独表中创建条目。

换句话说,这些是我的表:

cuisine:

objectId | name | country | spicy | fat |
-----------------------------------------

recipe:

objectId | name | ingredient | author |
---------------------------------------

CuisineRecipe:

objectId | cuisine | recipe |
-----------------------------

当一个新配方出现时,我想获取它的cuisineid 并在CuisineRecipe表中创建条目。

但是,在我的代码中,request.object.id返回undefined.

我猜这可能是因为在保存之前它没有 ID。但是,我尝试更改为afterSave,但仍然无法正常工作(即没有任何内容保存到 DB)

这是我的云代码:

var _ = require('underscore');

Parse.Cloud.beforeSave("Recipe", function(request, response) {
    // CREATE CuisineRecipe ENTRIES

    //getCuisines
    var cuisines = request.object.get("cuisines");
    // FOR EACH cuisines
    var promise = Parse.Promise.as();
    _.each(cuisines, function(CuisineObj){
        console.log(CuisineObj.text);

        // Verify that Cuisine exists
        var CuisineQuery = new Parse.Query("CuisinePublic");
        CuisineQuery.equalTo("CuisineId", CuisineObj.text);
        promise = promise.then(function(){
            return CuisineQuery.find({
                success: function(results){
                    console.log("results: " + results);
                    // If results is 1, it exists, everything is good. Proceed to adding CuisineRecipe entry
                    if (results.length == 1){
                        var CuisineRecipe = Parse.Object.extend("CuisineRecipe");
                        var CuisineRecipe = new CuisineRecipe();
                        CuisineRecipe.set("CuisineId", CuisineObj.text);
                        CuisineRecipe.set("Cuisine", results[0].id);
                        CuisineRecipe.set("Recipe", request.object.id);
                        CuisineRecipe.save();
                    }
                },
                error: function(results){
                    console.log("Error");
                    console.log("results: " + results);
                }
            });
        });

        return promise;
    });
    promise.then(function(){
        response.success();
    });
});
4

1 回答 1

0

我不一定了解您的不同班级及其关系,但request.object.id您需要指定班级,例如request.object.get("cuisines").id

于 2015-09-11T07:49:35.380 回答