2016-07-24 178 views
0

从这个问题的后续>Stopping response if document isn't found因为它被推荐我使用Promise。从猫鼬承诺返回回应

所以基本的前提是,如果我们无法在数据库中找到id,节点将返回“无法找到ID”消息。

v1.post("/", function(req, res) { 

    // If the project_id isn't provided, return with an error. 
    if (!("project_id" in req.body)) { 
     return res.send("You need to provide Project ID"); 
    } 

    // Check if the Project ID is in the file. 
    helper.documentExists(ProjectsData, {project_id: req.body.project_id}) 
     .then(function(c) { 
      if (c == 0) { 
       return res.send("The provided Project Id does not exist in our database."); 
      } else { 
       var gameDataObj = req.body; 

       GameData.addGameId(gameDataObj, function (err, doc) { 
        if (err) { 
         if (err.name == "ValidationError") { 
          return res.send("Please send all the required details."); 
         } 
         throw err; 
        }; 

        res.json(doc); 
       }) 
     }; 
    }); 
}); 

而且helper.documentExists

module.exports = { 
    documentExists: function(collection, query) { 
     return collection.count(query).exec(); 
    }, 
}; 

但脚本将继续在此之后运行并打印“未找到所需的数据”。

Output: 
    required data not found 
    1 

我正在使用原生ES6承诺。

var mongoose = require("mongoose"); 
    mongoose.Promise = global.Promise; 

编辑:包括整个获取路线。 (以后会解决这些抛ERR)

+1

'console.log(“required data not found”); ''''''''''''会运行任何计数,因为它超出了承诺然后回调 – Iceman

+0

哦,我认为Promise会在它低于它的任何东西之前运行。否则阻止? – DragoonHP

+0

是的,你应该。看到我的答案有点洞察力! – Iceman

回答

1
#######POINT 1######### 
ProjectsData.count({project_id: req.body.project_id}) 

    .then(function(c) { 
     #######POINT 3######### 
     if (c == 0) { 
      console.log("1"); 
      return res.send("The provided Project Id does not exist in our database."); 
      console.log("2"); 
     } 
    }); 
#######POINT 2######### 
//some other logic 
console.log("required data not found"); 

继异步工作流程:1点后,在创建承诺,您的附加处理程序。现在POINT 2将继续,而(在未来的某个时钟,承诺已解决,您将达到点3.

由于我对工作流程/目的的理解有限,我只是简单地将POINT 2代码放在ifelse{}在POINT 3(正如你在评论中正确猜测的那样) 编辑:感谢@ jfriend00指出了我以前版本的答案中的一个严重错误

+1

非常感谢。 :) – DragoonHP

+0

@DragoonHP - 这个答案是错误的。有绝对的保证,这个代码的输出将是“未找到需要的数据”,然后“1”和“2”不会因为“返回”而被击中。所有'.then()'处理程序在未来的时钟周期内保证异步(按照promoise标准),因此点2总是在1,2和3之间到达。 – jfriend00

+0

@ jfriend00您是对的。只有在下一个滴答声中才会触及承诺! – Iceman

1

您的代码基本上会导致在此:

ProjectsData.count().then(...); 
console.log("required data not found"); 

所以,当然第二console.log()要运行和打印。在.then()处理程序中没有发生任何事情,直到console.log()已经运行很久。即使如此,它也无法阻止其他代码的运行。承诺不会让口译员“等待”。他们只是为您提供结构来协调您的异步操作。

如果您想用承诺进行分支,那么您必须在.then()处理程序内进行分支,而不是在分支之后进行分支。


您没有足够展示您正在做什么以了解如何推荐完整解决方案。我们需要查看其余的请求,以帮助您根据异步结果进行正确的分支。


你可能需要的东西是这样的:

ProjectsData.count({project_id: req.body.project_id}).then(function(c) { 
    if (c == 0) { 
     return res.send("The provided Project Id does not exist in our database."); 
    } else { 
     // put other logic here 
    } 
}).catch(function(err) { 
    // handle error here 
}); 
+0

添加了一个可能的解决方案,但我们确实需要查看其他相关代码,以了解您实际正在尝试执行的操作。 – jfriend00

+0

无法在评论中发布整个代码,所以我会尽量让它失去相位>检查项目是否存在>如果是,请检查提供的数据是否有效>如果是,请将文档添加到mongodb集合中。 – DragoonHP

+1

@DragoonHP - 我问你在你的问题中使用“编辑”在那里包括相关的代码。当您展示整个问题并展示真实代码时,我们可以帮助更完全。 – jfriend00