Skip to content Skip to sidebar Skip to footer

Parse .then Method And Chaining | Syntax Eludes Me

Dear stackoverflow community, I am using application craft as a JS cloud IDE and integrating Parse's cloud database for storage. I'm writing a subscription service and want to chec

Solution 1:

Your syntax for handling then is incorrect, it should be:

query.find().then(

    function(results) {    
        console.log("Within the Then!");
        console.log(results);    
    },

    function(error){
        console.log("Could be an error"+error);
    }
);

the then() function takes one or two functions.

the first is a success handler, and the second is an error handler.

query.find().then(success,error)

This snippet is untested but should be pretty close.

var query = new Parse.Query(Parse.User);
query.equalTo(email, "me@me.com");  
query.count().then(
    function(resultsCount) {    
        //success handler
        if(resultsCount > 0){
            doSomeOtherFunction();
        }
    },
    function(error){
        //error handler
        console.log("Error:"+error);
    }
);

If you have more async work to do, your method should look similar to this, remember that when chaining promises the last then() should contain your error handling.

query.find().then(function(result){
  doSomethingAsync(); //must return a promise for chaining to work!
}).then(function(result1){
  doSomethingAsync2(); //must return a promise for chaining to work!
}).then(function(result2){
  doSomethingAsync3(); //must return a promise for chaining to work!
}).then(null,function(error){
  // an alternative way to handle errors
  //handles errors for all chained promises.
})

Post a Comment for "Parse .then Method And Chaining | Syntax Eludes Me"