解析云代码变量范围

时间:2014-12-07 03:23:04

标签: javascript asynchronous parse-platform scope cloud-code

我正在尝试通知组中的所有成员,包括该组的所有者,对成员资格进行更改。我有:

// Group: owner
// Membership: group, member

Parse.Cloud.afterSave("Membership", function(request) {
   var group = request.object;
   var owner = group("owner"); // I need to reference below in callback

   var q = new Parse.Query("Membership");
   q.equalTo("group", group);
   q.find().then(
      function(memberships) {
         var members = new Array;

         // include the owner in our push.
         members.push(owner); // <--- badness?

         var len = memberships.length;
         for (var i=0; i<len; ++i) {
           if (i in memberships) {
             var membership = memberships[i];
             var member = membership.get("member");
             members.push(member);
           }
         }

         var push = new Parse.Query(Parse.Installation);
         push.containedIn("members",members);
         // send the push, etc
      },
      function(error) { ... }
   );
});

是的,我考虑过让主人成为会员,这可能是更好的方式,但我在这里。如何从查询成功回调中引用组的所有者?重构这个afterSave()比重构我的模型更容易。

0 个答案:

没有答案