使用解析云函数

Posted

技术标签:

【中文标题】使用解析云函数【英文标题】:using parse cloud function 【发布时间】:2015-06-10 16:38:16 【问题描述】:

好的,我试图对与用户相关的对象进行平均,实现操作并在同一类的解析对象中返回结果。 我不知道这是否正确。

这是解析云代码:

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

            

        );

当我拨打电话时,我会收到日志(功能失败) 这是日志猫

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

【问题讨论】:

感谢回复 我上传了 logcat 谢谢你的帮助 【参考方案1】:

好的,问题出在这一行 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.

     
     );

【讨论】:

以上是关于使用解析云函数的主要内容,如果未能解决你的问题,请参考以下文章

解析云代码函数响应成功不是函数

解析云代码 query.withinKilometers

解析后保存云函数返回成功结果但不执行成功函数

在 Firebase 云函数中包含异步函数(eslint“解析错误:意外的令牌函数”)

解析云代码无效函数被调用

解析错误:Firebase 云函数中出现意外的令牌 selectWinner - Node.js V10