javascript – 解析云代码没有响应错误或使用promises成功

我的Parse云代码没有响应错误或成功.它只是超时,我不知道我做错了什么.它应该在不同的表中保存多个值,并完成向用户发送推送消息.

推送消息和用户表已调整并发送,但街道和分类帐对象未正确保存.此外,响应未被调用.

我过去做过(或者我很幸运).对我做错了什么的想法?

  var buyerId = request.params.buyerid;
  var sellerName = request.params.sellername;
  var streetId = request.params.streetid;
  var amount = request.params.amount;
  var pushMessageTemplate = request.params.pushMessage;

  var log = request ? request.log : console;
  var Streets = Parse.Object.extend('SHStreets');
  var streetQuery = new Parse.Query(Streets);
  streetQuery.equalTo("objectId", streetId);
  streetQuery.first({
    useMasterKey: true,
    success: function (street) {
      var streetName = street.get("name");

      var query = new Parse.Query(Parse.User);
      query.equalTo("objectId", buyerId);
      query.first({
        useMasterKey: true,
        success: function (user) {
          var promises = [];
          var now = new Date();

          var buyerName = user.get("username");
          // Set and save the change
          user.set("balance", user.get("balance") - amount);
          user.set("streets", user.get("streets") + 1);

          street.set("current_owner", user);
          street.set("owned_since", now);
          street.set("for_sale", false);
          street.set("price_bought", amount);

          var acl = new Parse.ACL();
          acl.setPublicReadAccess(true);
          acl.setWriteAccess(user, true);
          street.setACL(acl);

          //update ledger
          var Ledger = Parse.Object.extend("Ledger");
          var ledger = new Ledger();

          ledger.set("type", "buy");
          ledger.set("amount", amount);
          ledger.set('ledger_time', now);
          ledger.set("user", user);
          ledger.set("description", "x");

          promises.push(Parse.Object.saveAll([street, user, ledger], { useMasterKey: true }));

          // Find users with a given email
          var userQuery = new Parse.Query(Parse.User);
          userQuery.equalTo("objectId", user.id);

          var pushQuery = new Parse.Query(Parse.Installation);
          pushQuery.exists("user"); // filter out installations without users
          pushQuery.include('user'); // expand the user pointer
          pushQuery.matchesQuery("user", userQuery);

          // Send push notification to query
          promises.push(Parse.Push.send({
            where: pushQuery, // Set our installation query
            data: {
              alert: _.template(pushMessageTemplate)({
                sellername: sellerName,
                streetname: streetName,
                amount: amount
              })
            }
          }, {
              useMasterKey: true,
          }));

          return Parse.Promise.when(promises).then(function () {
            response.success("success");
          });
        },
        error: function (error) {
          log.error('buy-street error', error.message);
          response.error("Uh oh, buy request success failed." + error.message);
        }
      });
    },
    error: function (error) {
      log.error('buy-street error', error.message);
      response.error("Uh oh, buy request success failed." + error.message);
    }
  });
最佳答案
看起来您的第一个查询的成功函数不会返回任何内容:

streetQuery.first({
useMasterKey: true,
success: function (street) { // this function doesn't return anything
  // ...
  query.first({  // You probably want to return this promise
    useMasterKey: true,
    success: function (user) { // this is the function that you're returning to
      // ...
      return Parse.Promise.when(promises).then(function () {
        response.success("success");
      });
    },
  });
}

});

如果您没有return语句,Javascript将默认返回undefined.

转载注明原文:javascript – 解析云代码没有响应错误或使用promises成功 - 代码日志