1

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?

2 Answers 2

4

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);
});
Sign up to request clarification or add additional context in comments.

6 Comments

i've try it. but something 's value is undefined. this way is not different with my old code. response is in the function. i need something equalto response. because i need this value for other process and should be out of the function.
@AhmadDani you can use response only when its available, so if you want to do something that's dependent on response, you will have to place your response dependent code inside the callback function (some_cback) in this case... that's because your js code that's outside the callback will have executed before actual response from success callback of qyNewParse.find is returned.
Since the function has no return value, the assignment of var something = some_cback(..) is pointless. The callback passed as the parameter is the point of the example.
and my question is how to get return value of this query? anyone can explain me.... i'm just parse.com newbie..
@AhmadDani, I'm struggling with this very thing right now. Did you ever figure it out?
|
0

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); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.