using parse cloud function

106 views Asked by At

ok, im trying to make a average for the objects related with the user, realize the operations and return the results in a parse object of the same class. i dont know if this is correct.

this is the parse cloud code:

Parse.Cloud.define("Stats", function(request, response){

var query = new Parse.Query("stats");
query.equalTo("user", request.user);
query.select("Loses", "Wins", "Tilt", "speedKm","Races", "DaresCompleted");
query.find({

   success: function(results) {

      var LoseSum = 0;
      var WinSum = 0;
      var TiltSum = 0;
      var SpeedSum= 0;
      var TotalRace = 0;
      var TotalDares= 0;

      for (var i = 0; i < results.length; ++i) {
         LoseSum += results[i].get("Loses");
         WinSum += results[i].get("Wins");
         TiltSum += results[i].get("Tilt");
         SpeedSum += results[i].get("speedKm");
         TotalRace += results[i].get("Races");
         TotalDares += results[i].get("DaresCompleted");

         }

         var statspromed = Parse.Object.extend("statspromed");
         var mya = new statspromed();
         var num = results.length;
         mya.set("Loses", LoseSum);
         mya.set("Wins",WinSum);
         mya.set("Tilt", TiltSum/num);
         mya.set("speedKm", SpeedSum/num);
         mya.set("Races",TotalRace);
         mya.set("DaresCompleted", TotalDares);
         mya.set("user", request.user);
         mya.save().then(response.success(mya));

         },
      error: function() {
      response.error("movie lookup failed");
      }
      });

 });

and this is the call that i make to the function from android

  HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("user", ParseUser.getCurrentUser());
        ParseCloud.callFunctionInBackground("Stats", params, 
                             new FunctionCallback<ParseObject>() {

            public void done(ParseObject o, ParseException e) {
                if (e == null) {
                    Promed = o;

                } else {
                    Log.d("function", " fail");
                }

            }

        });

when i make the call i get the log (function fail) this is the logcat

06-10 14:26:36.384  17540-17540/sku1l.rideout_r I/Process﹕ Sending signal. PID: 17540 SIG: 9
06-10 14:28:54.591  20796-20822/sku1l.rideout_r D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-10 14:28:54.595  20796-20796/sku1l.rideout_r D/﹕ HostConnection::get() New Host Connection established 0xb42fdb60, tid 20796
06-10 14:28:54.603  20796-20796/sku1l.rideout_r D/Atlas﹕ Validating map...
06-10 14:28:54.669  20796-20822/sku1l.rideout_r D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so
06-10 14:28:54.670  20796-20822/sku1l.rideout_r D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-10 14:28:54.680  20796-20822/sku1l.rideout_r D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
06-10 14:28:54.691  20796-20822/sku1l.rideout_r D/﹕ HostConnection::get() New Host Connection established 0xb42fdff0, tid 20822
06-10 14:28:54.708  20796-20822/sku1l.rideout_r I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-10 14:28:54.757  20796-20822/sku1l.rideout_r D/OpenGLRenderer﹕ Enabling debug mode 0
06-10 14:28:54.843  20796-20822/sku1l.rideout_r W/EGL_emulation﹕ eglSurfaceAttrib not implemented
06-10 14:28:54.843  20796-20822/sku1l.rideout_r W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb4399e00, error=EGL_SUCCESS
06-10 14:28:55.201  20796-20796/sku1l.rideout_r D/este﹕ es 3 es
06-10 14:28:55.350  20796-20796/sku1l.rideout_r D/esto﹕ es 3 es
06-10 14:29:00.678  20796-20796/sku1l.rideout_r D/function﹕ fail
06-10 14:29:00.859  20796-20796/sku1l.rideout_r D/myAdwars﹕ es 2 es
06-10 14:29:01.070  20796-20796/sku1l.rideout_r D/mis adwards﹕ es 1 es

ok now im getting this from parse log

E2015-06-16T16:08:59.077Z]v53 Ran cloud function Stats for user N7ZTaawLVb    with

  Input: {"user":"N7ZTaawLVb"}
  Result: Uncaught Tried to save an object with a pointer to a new, unsaved objct.
  I2015-06-16T16:08:59.146Z]NaNloses
1

There are 1 answers

0
Eddy Charry On BEST ANSWER

ok the problem is in this line mya.save().then(response.success(mya));

JavaScript is asynchronous, the save() operation on "mya" has not finished by the time i try to response. so the correct way to do this is like this

mya.save(null, {
success: function(mya) {
    // The object was saved successfully.
     response.success(mya)
     },
     error: function(myComment, error) {
     // The save failed.

     }
     });