从 Mongoose promise 返回响应
Returning response from Mongoose promise
这个问题的跟进 > 因为有人推荐我使用 Promise。
所以基本前提,如果我们在数据库中找不到 id,我希望节点 return "Can't find 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();
},
};
但脚本在此之后继续 运行 并打印 "required data not found"。
Output:
required data not found
1
我正在使用原生 ES6 Promises。
var mongoose = require("mongoose");
mongoose.Promise = global.Promise;
编辑:包括整个获取路线。 (稍后会修复那些抛出的错误)
你的代码基本上是这样的:
ProjectsData.count().then(...);
console.log("required data not found");
因此,第二个 console.log()
当然会 运行 并打印。 .then()
处理程序 运行 中没有发生任何事情,直到 console.log()
已经 运行 很久之后。而且,即便如此,它也无法阻止来自 运行ning 的其他代码。承诺不会使解释器 "wait"。它们只是为您提供协调异步操作的结构。
如果你想用 promises 分支,那么你必须在 .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
});
#######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 点之后,创建了承诺并附加了您的处理程序。现在第 2 点将继续,而(在未来的某个时间,承诺已解决,您将到达第 3 点。
由于我对你的 workflow/purpose 的理解有限,我想说的是简单地将第 2 点代码放在第 3 点 if
的 else{}
中(正如你在评论中正确猜到的那样) ).
编辑:感谢@jfriend00 指出我之前版本的回答中的一个严重错误。
这个问题的跟进 >
所以基本前提,如果我们在数据库中找不到 id,我希望节点 return "Can't find 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();
},
};
但脚本在此之后继续 运行 并打印 "required data not found"。
Output:
required data not found
1
我正在使用原生 ES6 Promises。
var mongoose = require("mongoose");
mongoose.Promise = global.Promise;
编辑:包括整个获取路线。 (稍后会修复那些抛出的错误)
你的代码基本上是这样的:
ProjectsData.count().then(...);
console.log("required data not found");
因此,第二个 console.log()
当然会 运行 并打印。 .then()
处理程序 运行 中没有发生任何事情,直到 console.log()
已经 运行 很久之后。而且,即便如此,它也无法阻止来自 运行ning 的其他代码。承诺不会使解释器 "wait"。它们只是为您提供协调异步操作的结构。
如果你想用 promises 分支,那么你必须在 .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
});
#######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 点之后,创建了承诺并附加了您的处理程序。现在第 2 点将继续,而(在未来的某个时间,承诺已解决,您将到达第 3 点。
由于我对你的 workflow/purpose 的理解有限,我想说的是简单地将第 2 点代码放在第 3 点 if
的 else{}
中(正如你在评论中正确猜到的那样) ).
编辑:感谢@jfriend00 指出我之前版本的回答中的一个严重错误。