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

时间:2017-07-29 20:59:30

标签: javascript parse-platform promise parse-cloud

我的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);
    }
  });

2 个答案:

答案 0 :(得分:1)

看起来你的第一个查询的成功函数并没有返回任何内容:

undefined

});

如果您没有返回声明,Javascript默认会返回ABCMessage(MessageId(Tag100, Value100("abc"),MessageRepType(Tag200, TYPE1))

答案 1 :(得分:0)

我错过了返回承诺的函数错误。

 return Parse.Promise.when(promises).then(function () {
    // Set the job's sucess status
    response.success('success');
 },function(error) {
    response.error(error);
 });