使用解析云函数

using parse cloud function

好的,我正在尝试对与用户相关的对象进行平均,在相同的 class 的解析对象中实现操作和 return 结果。 不知道对不对

这是解析云代码:

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

 });

这是我从 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");
                }

            }

        });

当我打电话时,我得到了日志(功能失败) 这是 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

好的,现在我从解析日志中得到这个

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

好的问题出在这一行 mya.save().then(response.success(mya));

JavaScript 是异步的,"mya" 上的 save() 操作在我尝试响应时尚未完成。所以正确的做法是这样

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

     }
     });