在 afterSave 中使用 Promise

Using Promise in 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 SAVED. NOTIFICATIONS CHECK." 和 "NEW MOD ADDED> NOTIFICATIONS START." 都已注销。但是,之后我没有收到其他日志,也没有发送推送通知。我至少应该看看日志,看看 none。解析服务器在 afterSave 或其他东西中使用承诺有问题吗?为什么我的代码在遇到第一个承诺时似乎停止执行?

您的 afterSave() 云代码必须以 response.success()response.error(error)

结尾

你应该等待所有的 Promise 完成,然后用类似的东西来完成你的代码:

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

这原来是 Heroku 托管的问题。我还没有找到它。如果相同的代码托管在 AWS 或其他托管平台上,则不会出现这些问题。 Heroku 在其默认设置中使用的一些服务器设置可能需要更改。如果您遇到此问题并且有更多 "Switch off Heroku hosting" 之外的解决方案,请提交答案,我会接受。