parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Request timeout during updating User Object in cloud function

Open jaydeep82 opened this issue 10 years ago • 5 comments

Make sure these boxes are checked before submitting your issue -- thanks for reporting issues back to Parse Server!

Environment Setup

Heroku + MangoLab

Steps to reproduce

I'm facing issue while trying to update the User Object.

In console log, I can only see "before save" message and then after couple of seconds it shows error.

Parse.Cloud.define('updateUserData',function(request,response)
{    
    //var userObjectId = request.params.userId;
    var userObjectId = "BPAJJCNdR1";

    if(userObjectId == null){
        response.error("UserId should not be null");
    }

    var User = Parse.Object.extend("_User");
    var userQuery = new Parse.Query(User);

    userQuery.equalTo('objectId',userObjectId);

    userQuery.first({
        useMasterKey: true, 

        success:function(userData){

            console.log("before save");

            userData.set("status",2);
            userData.set("verificationCode", randomNumber);

            userData.save(null, { useMasterKey: true });

            console.log("after save");

        },
        error: function(error){
            console.log(error);
            response.error(error);
        }
    });


});

Logs/Trace

In heroku log, it shows Request timeout error.

2016-03-10T14:15:32.212544+00:00 heroku[router]: at=info method=POST path="/parse/classes/_User" host=myapp-dev2.herokuapp.com request_id=508de99b-3ac7-49d7-a295-9f7f8155b473 fwd="54.81.XX.XX" dyno=web.1 connect=1ms service=126ms status=200 bytes=1652
2016-03-10T14:15:32.223044+00:00 app[web.1]: before save
2016-03-10T14:16:02.001309+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/parse/functions/updateUserData" host=myapp-dev2.herokuapp.com request_id=5efb70e5-c942-4d65-a701-fb29f03763ef fwd="123.201.XX.XX" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0

I'm able to call all other cloud function.

Could you assist me here ?

Thank you in advance !!

jaydeep82 avatar Mar 11 '16 15:03 jaydeep82

One issue is that you don't call response.success in the success handler for your cloud function.... Now, that save is asynchronous, so you would expect the console log to print after it... but, you need to troubleshoot this better. randomNumber appears to be undefined.

Stop using callbacks, use promises, and use proper error handlers:

Parse.Cloud.define('updateUserData',function(request,response)
{    
  var userObjectId = "BPAJJCNdR1";
  var randomNumber = Math.floor((Math.random() * 10) + 1);

  if(userObjectId == null){
    response.error("UserId should not be null");
    return;
  }

  var userQuery = new Parse.Query('_User');
  userQuery.equalTo('objectId',userObjectId);

  userQuery.first({ useMasterKey: true }).then((userData) => {
    console.log("before save");
    userData.set("status",2);
    userData.set("verificationCode", randomNumber);
    return userData.save(null, { useMasterKey: true });
  }).then((userDataAgain) => {
    console.log('after save');
    response.success('whatever you want to return');
  }, (error) => {
    console.log(error);
    response.error(error);
  });

In addition, you should really test things locally, especially if you run in to problems... It would be a much faster turnaround to test local, and you can have live access to the unadulterated logs. Going to close this because I think you'll figure your issue out. If you find something real, please open a new issue. Thanks.

gfosco avatar Mar 16 '16 00:03 gfosco

Thank you @gfosco for your suggestion and comment. It's working now. I'll surely follow them. Thank you again !!

jaydeep82 avatar Mar 16 '16 12:03 jaydeep82

Parse.Cloud.define('All', function(request, status)
{ // res.success('ALL');

var query=new Parse.Query("MyCollection"); query.find().then(function (res) { console.log("after query is "+res);

for (var i=0; i< res.length;i++){ var expirydate=res[i].get('PExpirationDate');

var inputDate = new Date(expirydate); var todaysDate = new Date();

if((inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0))) { console.log("object id is"+res[i].get('_p_PurchasedUserID')); // alert("equal")

//res.success("object id is"+JSON.stringify(result)); } else {

//alert(" not equal") } } status.success("final result " + results);

}, function queryFailed(reason) {
  status.error("query unsuccessful, length of result " + result.length + ", error:" + error.code + " " + error.message);
     
});

});

The following error show on my Xcode Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

when i look into log it should display at=error code=H12 desc="Request timeout" method=POST path="/parse/functions/All" host=wellkeptbeauty.herokuapp.com request_id=a8938560-4f0b-44a6-9c36-2a1aff36cb9a fwd="183.82.109.220" dyno=web.1 connect=1ms service=30001ms status=503 bytes=0 protocol=https

wellkeptbeauty avatar Apr 27 '17 10:04 wellkeptbeauty

please help me out

wellkeptbeauty avatar Apr 27 '17 10:04 wellkeptbeauty

Does passing the session token or master key to the find() on the query solve the problem?

promisenxu avatar May 11 '17 04:05 promisenxu