我正在尝试向我的解析数据库添加唯一对象,也就是说,每个对象的name
属性必须不同。我尝试查询具有给定名称的所有对象,如此
var query = new Parse.Query(Food);
query.exists("name", name);
query.count({
success: function(number) {....}
但是,query.count
始终是存储在数据库中的对象总数(90),即使存在具有给定名称的0或1个对象。
编辑:
根据其中一个答案,我将代码修改为此。但是,我仍然在数据库中看到重复项。
var query = new Parse.Query(Food);
query.equalTo("name", name);
query.first({
success: function(results) {...}
以下是整个(编辑过的)功能
Parse.Cloud.define("recordFavorite", function(request, response) {
var foodList = request.params.foodList; //string array of food names
var foodListCorrected = new Array();
var Food = Parse.Object.extend("Food");
// Wrap your logic in a function
function process_food(i) {
// Are we done?
if (i == foodList.length) {
//console.log("count is " + foodListCorrected.length);
Parse.Object.saveAll(foodListCorrected, {
success: function(foodListCorrected) {},
error: function(foodListCorrected) {}
});
return;
}
var name = foodList[i];
//console.log("before name is " + name);
var query = new Parse.Query(Food);
query.equalTo("name", name);
query.first({
success: function(results) {
if(!results){
console.log("new");
var food = new Food();
food.set("name", name);
foodListCorrected.push(food);
// console.log(foodListCorrected.length);
} else {
//don't create new food
console.log("exists");
}
process_food(i+1)
},
error: function(error) {
console.log("error");
}
});
}
// Go! Call the function with the first food.
process_food(0);
});
编辑:
我已尝试使用promises来解决此问题的另一种方法,但行
return Parse.Object.saveAll(foodListCorrected);
似乎没有执行。我已经验证了foodListCorrected是一个非空数组。下面是该函数的完整代码。
Parse.Cloud.define("recordFavorite", function(request, response) {
var foodList = request.params.foodList; //string array of food names
var foodListCorrected = new Array();
var Food = Parse.Object.extend("Food");
var query = new Parse.Query(Food);
query.find().then(function(foods) {
for (i = 0; i < foodList.length; i++) {
var j;
for (j = 0; j < foods.length; j++){
if (foodList[i] == foods[j])
break;
}
if (j==foods.length)
foodListCorrected.push(foodList[i]);
}
console.log(foodListCorrected.length);
return Parse.Object.saveAll(foodListCorrected);
}).then(function() {
// Everything is done!
})
});
答案 0 :(得分:3)
试试这个:
Parse.Cloud.define("recordFavorite", function(request, response) {
var foodList = request.params.foodList; //string array of food names
var saveThese = [];
var FoodClass = Parse.Object.extend("Food");
for(var i = 0; i < foodList.length; ++i){
var food = new FoodClass();
food.set("name", foodList[i]);
saveThese.push(food);
}
Parse.Object.saveAll(saveThese, {
success: function(list){
response.success();
},
error: function(error){
response.error("Failure on saving food");
}
});
});
//If a food already exists in the database, then don't save it
Parse.Cloud.beforeSave("Food", function(request, response){
var query = new Parse.Query("Food");
query.equalTo("name", request.object.get("name"));
query.count({
success: function(count){
if(count > 0)
response.error("Food already exists");
else
response.success();
},
error: function(error){
response.error(error);
}
});
});
答案 1 :(得分:0)
&#34;存在&#34; condition接受一个参数,一个键的名称,并查看它是否存在于行中。因此,它将返回您的&#34; name&#34; column有一个值,任何值。你正在寻找的是这样的:
var query = new Parse.Query(Food);
query.equalTo("name", name);
query.first(...)