last day (15 days later) » 

10:29 PM
0
A: Parse.com Cloud Code, trying to filter query, but query still contains all objects

DehliTry this: Parse.Cloud.define("recordFavorite", function(request, response) { var successful = true; var count = 0; var foodList = request.params.foodList; //string array of food names var foodListCorrected = new Array(); for(var i = 0; i < foodList.length; ++i){ ...

 
The first time I ran the code, each iteration outputted 0. The second time I ran the code (with the same data), there was a combination of 1's and 2's, and a couple of errors. The third time, just 2's and 1's.
No new objects were created the second and third run.
 
So that means there are multiple rows with the same name. Do you want this? @Mahir
 
No, I want each row to have a unique name.
 
Why don't you use the object id instead? It's a guaranteed unique row attribute. If that's not possible, what are you trying to accomplish? How are food objects created?
 
I'm making a college dining app and I'm trying to store a list of all the foods that are served in the dining halls, which I scrape from the online menus. However, some of foods appear multiple times on the menus, and I don't want duplicates in the list.
 
10:29 PM
So wherever you save the food objects, if the count (from the code I provided) is 1, then don't save the new food object.
 
right, but the problem is that sometimes the count shows as 0 even when the object should exist in the query
 
Where is it that you create the Food objects? Is this the only place they're created?
 
Only in this function
 
Would you try my new function?
 
Now it doesn't save any objects. Also, I added var Food = Parse.Object.extend("Food"); to your code
I think the problem with your code is that the query and saveAll functions are running asynchronously, so saveAll is called before any objects are added to the array.
 
10:29 PM
Would you try again? This should make it wait until all the queries have been called.
 
Still nothing, surprsingly.
I tried adding console.log("length is " + foodList.length); right before the saveALl function, but the line is never called, so I think the while loop doesn't work.
 
Hello
 
hey, thanks for trying to help
So typically, you're supposed to address asynchronous code with promises
 
what do you mean by promises?
 
10:31 PM
thanks
and no problem!
i hope we can figure this out
 
So I posted my attempt at integrating promises in the SO question, but it was basically

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)
 
okay, that makes sense
let me step through that function
 
so the problem with that was that none of the objects were being saved
 
try this
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 (var i = 0; i < foodList.length; ++i) {
var j;
for (j = 0; j < foods.length; ++j){
if (foodList[i] == foods[j].get("name"))
break;
}
if (j==foods.length)
foodListCorrected.push(foodList[i]);
}
console.log(foodListCorrected.length);
notice the if statement
that's the only thing i changed
hopefully that will fix it
 
sorry, which one
 
10:38 PM
if (foodList[i] == foods[j].get("name"))
 
let me try
 
okay
 
but I do remember that foodList.length wasn't empty in the console.log statement
 
oh
interesting
actually that would make sense
because if that if statement wasn't working, then everything would be pushed
 
no, but it wasn't pushing everything, it was pushing nothing
yeah I just tried doing it your way and same results
the array count was 58
 
10:43 PM
awww :(
 
so I think the saveAll function just isn't being triggered for some reason
 
is the count supposed to be 58?
 
let me try it without the if statements to check
 
okay, thanks
 
without checking for duplicates, it was 61
which seems low
 
10:46 PM
gmmm
could you test it with a smaller data size?
so that way we can test to see if it's working
 
yeah
hold on
 
take your time :)
 
so i still haven't reduced the data size yet
but I just tried the old function (the non promise one)
 
did it give different results?
 
and I checked the database, and at first it said 58 objects
but then I refreshed, and it changed to 175
 
10:53 PM
what does that mean?
 
well, 58 was supposedly the number of unique objects
 
oh, so it added duplicates?
 
so it's as if it saves the unique 58 and then adds more
 
i'm not very familiar with the saveall function
i'll google it
 
also, it doesn't add duplicates if I rerun the code
 
10:54 PM
the new function?
that's good
 
it's only a problem the first time
 
ahhh
 
not the new function, the old function
the old function adds duplcates the first time, but no new objects are added when I rerun the code
 
wait
can i give another option?
 
sure
 
10:56 PM
give me a minute to write 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(number){
if(count > 0)
response.error();
else
response.success();
},
error: function(error){
response.error();
}
});
});
would you try that
and remove your old function
 
wait, so is this the whole thing
 
yes
you just save it
 
because it seems like it only works for 1 food
 
right
every time a food is saved
that function is executed
just try it :)
 
So I'm still a little confused
 
11:05 PM
Every time a food is saved, that function is called
 
do I still call the recordFavorite function
or do I replace it with the new one
 
what you need to do is save all the foods
but you shouldn't care about whether they are a duplicate
so don't filter the list
just save the whole list
and then my function will make sure only the non duplicates are saved
 
and it's triggered automatically?
 
yes
 
So I just tried

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) {
return Parse.Object.saveAll(foods);
}).then(function() {
// Everything is done!

})

});



Parse.Cloud.beforeSave("Food", function(request, response){
var query = new Parse.Query("Food");
query.equalTo("name", request.object.get("name"));
and it didn't save any objects
 
11:12 PM
wait
shouldn't you be saving foodList?
 
oh yeah, sorry
Parse.Cloud.define("recordFavorite", function(request, response) {

var foodList = request.params.foodList; //string array of food names
Parse.Object.saveAll(foodList);


});
it didn't save anything
but I removed the beforeSave function, and it still didn't save anything
 
keep the beforesave function
and do this
give me another minute
haha
 
So I just noticed that the recordFavorite function is called multiple times within a loop
 
ahhh
hold on
 
so I'm wondering if there's some asynchronous behavior I need to address with that
 
11:18 PM
give me 30 seconds :)
 
yeah sure no rush
 
try this
Parse.Cloud.define("recordFavorite", function(request, response) {
var foodList = request.params.foodList; //string array of food names
var success = true;
for(var i = 0; i < foodList.length; ++i){
var food = Parse.Object.extend("Food");
food.set("name", foodList[i]);
food.save(null, {
success: function(food){},
error: function(error){success = false;}
});
}
if(success)
response.success();
else
response.error();
});

Parse.Cloud.beforeSave("Food", function(request, response){
var query = new Parse.Query("Food");
 
Failed with: TypeError: Object function (){a.apply(this,arguments)} has no method 'set'
 
ahhh
one moment
Parse.Cloud.define("recordFavorite", function(request, response) {
var foodList = request.params.foodList; //string array of food names
var success = true;
for(var i = 0; i < foodList.length; ++i){
var FoodClass = Parse.Object.extend("Food");
var food = new FoodClass();
food.set("name", foodList[i]);
food.save(null, {
success: function(food){},
error: function(error){success = false;}
});
}
if(success)
response.success();
else
response.error();
});

Parse.Cloud.beforeSave("Food", function(request, response){
try that instead
var FoodClass = Parse.Object.extend("Food");
var food = new FoodClass();
those are the two lines that were changed
 
11:35 PM
now it's giving this error for each iteration
before_save triggered for Food
Input: {"original":null,"update":{"name":"Cream of Tomato Soup"}}
Result: ReferenceError: count is not defined
at query.count.success (main.js:383:4)
at Parse.js:2:5906
at e (Parse.js:2:5101)
at Parse.js:2:4651
at Array.forEach (native)
at Object.x.each.x.forEach [as _arrayEach] (Parse.js:1:665)
at c.extend.resolve (Parse.js:2:4602)
at e (Parse.js:2:5237)
at Parse.js:2:4651
at Array.forEach (native)
 
good
we're getting somewhere
oops
that was a silly error
replace count with number
 
E2014-09-02T23:36:59.373Z] v194: before_save triggered for Food
Input: {"original":null,"update":{"name":"Miso Soup"}}
Result: TypeError: Object #<Object> has no method 'number'
at main.js:381:7
I thought count was correct
 
would you show me your code
query.count({
success: function(count){
if(count > 0)
response.error();
else
response.success();
},
error: function(error){
response.error();
}
});
should be like that
if you wanted to use count
count or number are passed as parameters to the success function
 
11:55 PM
any luck?
 

  last day (15 days later) »