0

我正在尝试在云代码中执行以下代码:

    //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”。和“新MOD添加>通知开始。” 都已注销。但是,在那之后我没有收到其他日志,也没有发送任何推送通知。我至少应该看到日志,但什么也看不到。在 afterSave 或其他东西中使用 promises 解析服务器是否存在问题?为什么我的代码在达到第一个承诺时似乎停止了执行?

4

2 回答 2

0

事实证明这是 Heroku 托管的问题。我没有追查过去。如果相同的代码托管在 AWS 或其他托管平台上,则不会发生这些问题。Heroku 在其默认设置中使用的一些服务器设置可能需要更改。如果您遇到此问题并且除了“关闭 Heroku 托管”之外还有更多解决方案,请提交答案,我会接受。

于 2017-02-06T17:58:15.350 回答
0

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

您应该等待所有 Promises 完成结束,然后使用以下内容完成您的代码:

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}));
});
于 2017-01-23T09:17:33.747 回答