客户提交新的recipe
. 该recipe
对象包括一个数组,cuisines
该数组类似于此配方所属的类别。
在beforeSave()
Cloud Code 函数中,我想在具有recipe
s 和cuisine
s 的单独表中创建条目。
换句话说,这些是我的表:
cuisine:
objectId | name | country | spicy | fat |
-----------------------------------------
recipe:
objectId | name | ingredient | author |
---------------------------------------
CuisineRecipe:
objectId | cuisine | recipe |
-----------------------------
当一个新配方出现时,我想获取它的cuisine
id 并在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();
});
});