Node.js 猫鼬承诺的回应

Node.js 猫鼬承诺的回应,node.js,express,mongoose,promise,es6-promise,Node.js,Express,Mongoose,Promise,Es6 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 Projec

这个问题的后续行动>因为建议我使用承诺

所以基本前提是,如果我们在数据库中找不到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.document存在

module.exports = {
    documentExists: function(collection, query) {
        return collection.count( query ).exec();
    },
};
但脚本在此之后继续运行,并打印“未找到所需数据”

我使用的是本地ES6承诺

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

编辑:包括整个获取路径。(稍后将修复这些抛出错误)

您的代码基本上会导致以下结果:

ProjectsData.count().then(...);
console.log("required data not found");
当然,第二个
console.log()
将运行并打印。在
控制台.log()
已经运行很久之前,
.then()
处理程序中不会发生任何事件。而且,即使这样,它也无法阻止其他代码运行。承诺不会让口译员“等待”。它们只是为您提供了协调异步操作的结构

如果要使用承诺进行分支,则必须在
.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
});
遵循异步工作流:在第1点之后,创建承诺并附加处理程序。现在,第2点将继续,而(在未来的某个时间点)承诺已得到解决,您将达到第3点

由于我对您的工作流程/目的了解有限,我想简单地将第2点代码放在第3点的
if
else{}
中。
编辑:感谢@jfriend00指出了我以前版本答案中的一个严重错误。

console.log(“未找到所需数据”);
将运行任何计数,因为它超出了承诺,然后回调。哦,我假设承诺将在低于承诺的任何东西之前运行。我的假设错了吗?我应该在else块下添加其余的逻辑吗?是的,你应该。请参阅我的答案以了解一些细节!!如果承诺让你的代码等待它们,那么你会让它们变得毫无用处,不是吗?;)这取决于你的依赖性。基于此,您可以分支代码。这有助于您注意,只有关键区域等待承诺得到解决,而其他区域则继续不受干扰。添加了一个可能的解决方案,虽然我们确实需要查看其余的相关代码才能知道您实际上在做什么。无法在注释中发布整个代码,因此我将尝试对其进行分段>检查项目是否存在>如果是,检查提供的数据是否有效>如果是,将文档添加到mongodb集合。@DragoonHP-我要求您使用“编辑”在您的问题中,请在此处包含相关代码。当你展示整个问题并展示真实代码时,我们可以提供更全面的帮助。很抱歉给你带来这么多困惑。谢谢你的帮助。真的很欣赏。:)@龙骑兵-冰人已经纠正了他们的答案,我称之为错误的解释。如果您的代码正在运行,并且您现在明白了原因,那么您就一切就绪。@DragoonHP-这个答案是错误的。绝对保证此代码的输出将是“未找到所需数据”,然后“1”和“2”不会被命中,因为
返回
。所有
.then()
处理程序在未来的时钟周期内都保证异步(根据promoise标准),因此在1、2和3之间总是首先到达第2点。@jfriend00您是对的。承诺最多只能在下一刻实现!!
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
});
#######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");