Reputation: 65
So im doing a simple query for an object and I am just wondering why when I call the .find() method on query object it returns success even though its an empty array? Here is a sample of my code.
var HuntObject = Parse.Object.extend("HuntObject",{
defaults: {
title: "hunt",
startDate: "",
endDate: "",
prize: "",
players: [],
items: []
}
});
var huntinfo = new Parse.Query(HuntObject);
var user = Parse.User.current().get("username");
huntinfo.notEqualTo("players" , user);
huntinfo.equalTo("title", huntItem);
huntinfo.find({
success: function(results) {
console.log("results")
Console log would give me results = [] I suppose I could just check if results is an empty array and then go from there but I thought that is what using .find was for. Any help would be appreciated.
Upvotes: 0
Views: 2572
Reputation: 546
parse.com would not throw error even if an empty set is retrieved.
A find query will always go in success method if it has been executed properly even if no results are retrieved.
You can check the error using the error code.
var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);
query.equalTo("playerName", "Dan Stemkoski");
query.find({
success: function(results) {
alert("Successfully retrieved " + results.length + " scores.");
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
alert(object.id + ' - ' + object.get('playerName'));
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}});
So if you get an empty array perform required operations in success method only.
Upvotes: 7