2017-07-29 83 views
4

我的解析雲代碼未響應錯誤或成功。它只是超時,我不知道我做錯了什麼。它應該在不同的表格中保存多個值,並向用戶發送推送消息。解析雲代碼未響應錯誤或承諾成功

推送消息和用戶表被調整併發送,但街道和分類帳對象未被正確保存。此外,響應沒有被調用。

我在過去工作過(或者我只是幸運的)。任何想到我做錯了什麼?

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

回答

0

我錯過了承諾返回的函數錯誤。

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

它看起來像你的第一個查詢的成功函數不返回任何東西:

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

+0

感謝您的回覆,我真的不得到什麼在你的答案不同,我是回'Parse.Promise.when(承諾)。然後(函數()'吧? – Lengo

+0

的代碼我答案與你的相同,只是突出興趣點。「你可能想要返回這個承諾」的行應該是'return query.first({' – adamdport

+0

@Lengo是否有意義? – adamdport

相關問題