Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在afterSave中使用承诺_Node.js_Promise_Parse Server_After Save - Fatal编程技术网

Node.js 在afterSave中使用承诺

Node.js 在afterSave中使用承诺,node.js,promise,parse-server,after-save,Node.js,Promise,Parse Server,After Save,我尝试在云代码中执行以下代码: //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.exist

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

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

您的
afterSave()
云代码必须以
响应结束。success()
响应。错误(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}));
});

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

在第一个
的顶部放置一个console.log。然后
查看它是否至少得到了解决there@JaromandaX添加它们。承诺中的日志在我的服务器日志中不可见。我看到的最后一个日志是新的MOD ADDED NOTIFICATIONS开始日志。这是一个afterSave。我在afterSave中没有响应。奇怪的是,我定义了一些afterSave()方法如下:
{Parse.Cloud.afterSave(“MyClass”,函数(请求,响应){
并且它正在处理signatureI中的响应我的问题与我目前的问题相同,但是检查文档进行解析,afterSave不包括响应。您的函数可能会自动崩溃,您应该测试添加更多的console.log以尝试定位代码中的错误。我有,请参见上文。它不包含响应r表示失败函数成功。它在这些点之前失败。