2017-01-22 69 views
0
使用無極

我試圖做的雲代碼下面的代碼:「 MOD SAVED NOTIFICATIONS CHECK」在afterSave

//Code for sending push notifications after an update is created 
Parse.Cloud.afterSave('AssignmentMod', function(request){ 
    //Only send if the mod is new 
    console.log("MOD SAVED. NOTIFICATIONS CHECK.") 
    if(!request.object.existed()) { 
    console.log("NEW MOD ADDED. NOTIFICATIONS START.") 
    var mod = request.object 

    //Get the users who have these classes 
    var userType = Parse.Object.extend('User') 
    var usersQuery = new Parse.Query(userType) 
    usersQuery.equalTo('currentClasses', mod.get("parentClass")) 

    //Get the users who removed the assignment 
    var modsType = Parse.Object.extend('AssignmentMod') 
    //If if was an assignment and removed at some point 
    var mods1Query = new Parse.Query(modsType) 
    mods1Query.notEqualTo("assignment", null) 
    mods1Query.equalTo("assignment", mod.get("assignment")) 
    mods1Query.equalTo("type", "remove") 
    //If it was an assignment mod and was removed at some point 
    var mods2Query = new Parse.Query(modsType) 
    mods2Query.notEqualTo("assignmentMod", null) 
    mods2Query.equalTo("assignmentMod", mod.id) 
    mods2Query.equalTo("type", "remove") 
    //Get the remove mods for this particular update 
    var modsQuery = new Parse.Query.or(mods1Query,mods2Query) 

    //Run the user and mods queries 
    Parse.Promise.when(
     usersQuery.find(), 
     modsQuery.find() 
    ).then(function(users, removeMods) { 
     console.log("QUERY 1 COMPLETE.") 
     //Get all users that copied this remove mod 
     var copyType = Parse.Object.extend('ModCopy') 
     var copyQuery = new Parse.Query(copyType) 
     copyQuery.containedIn("assignmentMod", removeMods) 
     copyQuery.find().then(function(copies){ 
     console.log("QUERY 2 COMPLETE.") 
     var copyUsers = copies.map(function(copy){ 
      return copy.get("user") 
     }) 

     //Get the devices of users that belong to the class, did not remove the assignment, and have an ios devices 
     var deviceQuery = new Parse.Query(Parse.Installation) 
     deviceQuery.equalTo("deviceType", "ios") 
     deviceQuery.containedIn("user", users) 
     deviceQuery.notContainedIn("user", copyUsers) 

     //Send the push notification 
     console.log("PUSHING NOTIFICATIONS") 
     Parse.Push.send(
      { 
      where: deviceQuery, 
      data: { 
       alert: "You have new updates." 
      } 
      }, 
      {userMasterKey: true} 
     ).then(
      function(){ 
      console.log("SUCCESSFULLY SEND NOTIFICATIONS.") 
      }, 
      function(error){ 
      throw "Error sending push notifications:" + error.code + " : " + error.message 
      console.log("FAILED TO SEND NOTIFICATIONS") 
      } 
     ) 

     }, 
     function(reason){ 
     throw "Error: " + reason 
     print("ERROR: " + reason) 
     }) 

    }, 
    function(reason){ 
     throw "Error: " + reason 
     print("ERROR: " + reason) 
    }) 

    } 

}) 

我可以在我的日誌,即見和「新增MOD>通知開始」。都被註銷。但是,此後我沒有收到其他日誌,也沒有發送推送通知。我應該在leas看到日誌,並沒有看到。使用afterSave或其他內容中的promise來分析服務器是否存在問題?爲什麼我的代碼在碰到第一個承諾時似乎停止執行?

+0

在第一個'.then'的頂部放置一個console.log,看它是否至少到達 –

+0

@JaromandaX添加它們。承諾內的日誌在我的服務器日誌中不可見。我看到的最後一個日誌是NEW MOD ADDED NOTIFICATIONS START日誌。 – steventnorris

回答

0

事實證明,這是一個與Heroku託管問題。我沒有追查過去。如果相同的代碼託管在AWS或其他主機平臺上,則不會發生這些問題。有可能Heroku在默認設置中使用了某些服務器設置,需要更改。如果你遇到這個問題,並有更多的解決方案超越「關閉Heroku託管」,然後提交一個答案,我會接受。

0

afterSave()雲代碼都必須通過結束了response.success()response.error(error)

您應該等待所有承諾徹底結束然後像完成你的代碼:

promise.then(function(){ 
    console.log("END of afterSave() " ; 
    response.success(); 
}, function(error){ 
    console.error("error in afterSave() " + " : " + error.message); 
    response.error(JSON.stringify({code: error.code, message: error.message})); 
}); 
+0

這是一個afterSave。在afterSave中我沒有迴應。 – steventnorris

+0

奇怪的是我定義了一些我的afterSave()方法是這樣的:'{Parse.Cloud.afterSave(「MyClass」,function(request,response){'它與簽名中的響應 – toofoo

+0

一起工作與我目前遇到的相同問題,但檢查文檔的解析,afterSave不包括響應。 – steventnorris

相關問題