【发布时间】:2015-07-22 16:20:21
【问题描述】:
我有这个代码用于通过数据库进行统计报告。
exports.calculate = function(req, res, next) {
models.Quiz.count()
.then(function(questions) {
statistics.questions = questions;
models.Comment.count().then(function(comments) {
statistics.comments = comments;
statistics.average_comments = (statistics.comments / statistics.questions).toFixed(2);
models.Quiz.findAll({
include: [{model: models.Comment}]})
.then(function(quizes) {
for (index in quizes) {
if (quizes[index].Comment.length) {
statistics.commented_questions++;
} else {statistics.no_commented++;}
};
})
})
})
.catch(function(error) {next(error)})
.finally(function() {next()});
};
在 SQL 语句之前它可以正常工作,但从不循环 for,所以我永远无法得到 p>
statistics.commented_questions
或
statistics.no_commented
先谢谢了!
【问题讨论】:
-
您的
models.Quiz.findAll()呼叫可能收到错误响应?如果代码只是在某个点停止,那么您可能会遇到某种运行时错误,您需要添加足够的调试代码来查看错误的位置/内容。您可能还想用一些可以吸引可能真正了解您正在使用的 API 的人的东西来标记您的问题,因为这个问题可能特定于您如何使用这些 API。而且,您的标题可能应该更具体地说明您的代码失败的地方(再次吸引可能能够提供帮助的合适类型的人)。 -
感谢您的建议!对不起我的英语。下次我会努力做得更好。
-
我的 cmets 是关于这个问题的,而不仅仅是下次。您可以随时使用“编辑”链接来澄清或改进您的问题。
-
你需要
return你的(回调)函数的承诺!
标签: javascript express promise