使用解析云功能

时间:2015-06-10 16:38:17

标签: javascript android parse-platform 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");
      }
      });

 });

这是我对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

1 个答案:

答案 0 :(得分:0)

好吧问题出在这一行     mya.save(),然后(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.

     }
     });