Reputation: 251
in Parse.Com i usually get value of query in the function. How to get it out side of the function?
var TableObject = Parse.Object.extend(TableId);
var qyNewParse = new Parse.Query(TableObject);
qyNewParse.doesNotExist("last_sync");
qyNewParse.find({
success: function(results) {
},
error: function(error){
}
});
var something = results.length
for example if i want to get result.length at outside of function. How to get var something value?
Upvotes: 1
Views: 1582
Reputation: 2198
If you're using angular here's an example code you could use. It's handled properly with promises ($q), so basically your code will "wait" until promise if fulfilled and then move forward
runParseFunction = function(param) {
var deferred = $q.defer();
Parse.Cloud.run('myParseFunction', { param: param }, {
success: function(response) {
deferred.resolve(response);
},
error: function(error) {
deferred.reject(error);
}
});
return deferred.promise;
};
Than to call it outside in any other place you just do:
runParseFunction(param).then(function(response) {
console.log(response);
});
Upvotes: 0
Reputation: 100175
You can't have a function return the result of a query, because the function will always return before the query is completed. Instead of having your function return a value, you should give the function its own callback parameter that will be called when the query completes, like:
function some_cback(callback) {
var TableObject = Parse.Object.extend(TableId);
var qyNewParse = new Parse.Query(TableObject);
qyNewParse.doesNotExist("last_sync");
qyNewParse.find({
success: function(results) {
callback(results); //call the callback
},
error: function(error){
}
});
}
//call the function
var something = some_cback(function(response) {
console.log(response);
});
Upvotes: 4