3

I am facing 2 issues with writing a background job in parse

Here is my code

Parse.Cloud.job("createSilentUsers",function(request,response){

 // Set up to modify user data
  Parse.Cloud.useMasterKey();

    //get all the users from backupusers table where isbiscootactivated = 0 and issnsactivated=0
     // Query for all users
  var query = new Parse.Query("biscootusers");
  query.equalTo("isbiscootactivated",0);
  query.equalTo("issnsactivated",0);
  query.first({
                  success: function(result) {
                    // Successfully retrieved the object.
                    var objUser = result;
                    console.log(result.attributes.deviceid);                        
                    console.log(result.attributes.imei);
                    console.log(result.attributes.appname);
                    console.log(result.attributes.appid);
                    console.log(result.attributes.appversion);

                    //check if the deviceid and imei set is already a biscoot activated user
                    var promise = Parse.Promise.as();                   
                    promise = promise.then(function() {
                            console.log("we are inside the prmise");
                            return Parse.Cloud.httpRequest({
                                              method: 'POST',
                                              url: 'http://<our server name>/1.0/PartnerActivation/isDeviceExists',
                                              headers: {
                                                        'Content-Type': 'application/x-www-form-urlencoded'},
                                              body: {
                                    imei: result.attributes.imei,
                                    deviceid: result.attributes.deviceid,
                                    appname: result.attributes.appname,
                                        appid: result.attributes.appid,
                                        appversion: result.attributes.appversion}
                                            }).then(function(httpResponse) 
                                                    {
                                                       console.log("Response of isdeviceactivated is " + httpResponse.text);
                                                       if(httpResponse.text == 'true' || httpResponse.text="True")
                                                            {                                                                       
                                                                console.log("The user is already activated");           
                                                                objUser.set("isbiscootactivated",1);
                                                                objUser.save();
                                                            }
                                                            else
                                                            {
                                                                //do the biscoot activation here
                                                                console.log("its not activated, lets do the biscootusers activation");
                                                            }
                                                     }, 
                                                    function(error) {
                                                      console.log("error occurred during isDeviceExists api as " + error);
                                                    });                                                
                                                  });                                               

                            console.log("nothing seems to have happened");

                  },
                  error: function(error) {
                    console.log("Error: " + error.code + " " + error.message);
                  }
        }).then(function() {
                    // Set the job's success status
                    status.success("All the users been set to the db successfully");
  }, function(error) {
        // Set the job's error status
        status.error("Uh oh, something went wrong.");
  });   
});

The Issues I have are

  1. In the logs I frequently see this error

    Ran job createSilentUsers with: Input: {} Failed with: ReferenceError: status is not defined at main.js:74:9 at r (Parse.js:2:4981) at Parse.js:2:4531 at Array.forEach (native) at Object.E.each.E.forEach [as _arrayEach] (Parse.js:1:666) at n.extend.resolve (Parse.js:2:4482) at null. (Parse.js:2:5061) at r (Parse.js:2:4981) at n.extend.then (Parse.js:2:5327) at r (Parse.js:2:5035)

  2. The http request just doesn't seem to work, while it always does if I test it from some http REST client.

6
  • I'm guessing that it is this line that goes wrong because status is undefined. status.success("All the users been set to the db successfully"); (Or for that matter the one below when an error occurs and you also use status). Commented Feb 24, 2014 at 5:38
  • yes, but "status" is mandatory stuff with parse background jobs, otherwise it won't log as successful in the logs, its also being said is samples parse.com/docs/cloud_code_guide#jobs "As with other Cloud Functions, you should handle success and error conditions. For Background Jobs, you do this by calling either status.success() or status.error() when your function completes. Your job execution status will then be set to completed. If you don't call either of these methods, your job will time out in 15 minutes." Commented Feb 24, 2014 at 5:41
  • 1
    True, but they use status as their second argument to the inner function, you have response there. So in your cause it would be response.success(); Commented Feb 24, 2014 at 6:04
  • oh, yes, foolish stuff man, thanks!, let me test Commented Feb 24, 2014 at 6:32
  • you were right, the status issue is no more, but the http request fails to work Commented Feb 24, 2014 at 6:56

1 Answer 1

2

Just change the "response" to "status" on the funcion header.

Parse.Cloud.job("createSilentUsers",function(request,response){

to this

Parse.Cloud.job("createSilentUsers",function(request,status){
Sign up to request clarification or add additional context in comments.

1 Comment

Had same issue. So simple to solve but still took me hours to find this great answer! Thank you!

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.