app.get('/ratings', function (req, res){
db.database.find({rating:5}).count(function(err, doc) {
review.FiveStarCount=doc;
});
db.database.find({rating:4}).count(function(err, doc) {
review.FourStarCount=doc;
});
I am new to using the MEAN stack and trying to return the values of the number of 5 and 4 star reviews. I can retrieve the number of 5 stars just fine, but when I do multiple , I seem to be running into trouble and not sure how to set it up.
我是新手使用MEAN堆栈并尝试返回5和4星评价的数值。我可以很好地检索5星的数量,但是当我做多个时,我似乎遇到麻烦而不确定如何设置它。
2 个解决方案
#1
0
You kind of need to get the gist of how to handle control flow while doing async operations. The function execution would not stop, unlike php, when you are running an async operation. To accomplish what you are trying to do, the second db call needs to be inside the callback of the first. Try this out.
您需要了解如何在执行异步操作时处理控制流的要点。当您运行异步操作时,与php不同,函数执行不会停止。要完成您要执行的操作,第二个db调用需要位于第一个db回调内。试试吧。
app.get('/ratings', function (req, res, next) {
db.database.find({rating:5}).count(function(err, doc) {
// always handle error first
if (err) {
return next(err);
}
if (doc) {
review.FiveStarCount = doc;
}
db.database.find({rating:4}).count(function(err, doc) {
if (err) {
return next(err);
}
if (doc) {
review.FourStarCount = doc;
}
console.log(review.FiveStarCount, review.FourStarCount);
});
});
});
#2
0
So it seems like you are running against the async nature of JavaScript. in the case of the functions above, they will not execute in the order you have written (and you wouldn't want them too!) because they are async. There are few simple solutions to get you going while you learn more about the problem.
所以看起来你正在反对JavaScript的异步性质。在上述函数的情况下,它们不会按照您编写的顺序执行(并且您也不会想要它们!)因为它们是异步的。在您了解有关问题的更多信息时,几乎没有简单的解决方案可以让您前进。
Solution 1: Nested callbacks. Hard to read. also, it waits for the first to complete before firing the second, possibly slowing everything down.
解决方案1:嵌套回调。难以阅读。此外,它等待第一个完成,然后开始第二个,可能减慢一切。
app.get('/ratings', function(req, res) {
var review = {};
db.database.find({rating:5}).count(function(err, doc) {
review.FiveStarCount = doc;
db.database.find({rating:4}).count(function(err, doc) {
review.FourStarCount = doc;
// DO something with data
res.json(review);
});
});
Solution 2: Latch. This has the advantage of making the requests in parallel and not being as awful to read.
解决方案2:闩锁。这样做的好处是可以并行处理请求,而不会让读取变得太糟糕。
app.get('/ratings', function(req, res) {
var review = {};
db.database.find({rating:5}).count(function(err, doc) {
review.FiveStarCount=doc;
if (review.FiveStarCount && review.FourStarCount) {
// DO something here
// Will only execute if the other has finished
}
});
db.database.find({rating:4}).count(function(err, doc) {
review.FourStarCount=doc;
if (review.FiveStarCount && review.FourStarCount) {
// DO something here
// Will only execute if the other has finished
}
});
});
There are course more solutions, such as maybe a better query or promises. Maybe read a bit more about async JavaScript here to get a better understanding.
当然有更多的解决方案,例如更好的查询或承诺。也许在这里阅读更多关于异步JavaScript的内容以获得更好的理解。
#1
0
You kind of need to get the gist of how to handle control flow while doing async operations. The function execution would not stop, unlike php, when you are running an async operation. To accomplish what you are trying to do, the second db call needs to be inside the callback of the first. Try this out.
您需要了解如何在执行异步操作时处理控制流的要点。当您运行异步操作时,与php不同,函数执行不会停止。要完成您要执行的操作,第二个db调用需要位于第一个db回调内。试试吧。
app.get('/ratings', function (req, res, next) {
db.database.find({rating:5}).count(function(err, doc) {
// always handle error first
if (err) {
return next(err);
}
if (doc) {
review.FiveStarCount = doc;
}
db.database.find({rating:4}).count(function(err, doc) {
if (err) {
return next(err);
}
if (doc) {
review.FourStarCount = doc;
}
console.log(review.FiveStarCount, review.FourStarCount);
});
});
});
#2
0
So it seems like you are running against the async nature of JavaScript. in the case of the functions above, they will not execute in the order you have written (and you wouldn't want them too!) because they are async. There are few simple solutions to get you going while you learn more about the problem.
所以看起来你正在反对JavaScript的异步性质。在上述函数的情况下,它们不会按照您编写的顺序执行(并且您也不会想要它们!)因为它们是异步的。在您了解有关问题的更多信息时,几乎没有简单的解决方案可以让您前进。
Solution 1: Nested callbacks. Hard to read. also, it waits for the first to complete before firing the second, possibly slowing everything down.
解决方案1:嵌套回调。难以阅读。此外,它等待第一个完成,然后开始第二个,可能减慢一切。
app.get('/ratings', function(req, res) {
var review = {};
db.database.find({rating:5}).count(function(err, doc) {
review.FiveStarCount = doc;
db.database.find({rating:4}).count(function(err, doc) {
review.FourStarCount = doc;
// DO something with data
res.json(review);
});
});
Solution 2: Latch. This has the advantage of making the requests in parallel and not being as awful to read.
解决方案2:闩锁。这样做的好处是可以并行处理请求,而不会让读取变得太糟糕。
app.get('/ratings', function(req, res) {
var review = {};
db.database.find({rating:5}).count(function(err, doc) {
review.FiveStarCount=doc;
if (review.FiveStarCount && review.FourStarCount) {
// DO something here
// Will only execute if the other has finished
}
});
db.database.find({rating:4}).count(function(err, doc) {
review.FourStarCount=doc;
if (review.FiveStarCount && review.FourStarCount) {
// DO something here
// Will only execute if the other has finished
}
});
});
There are course more solutions, such as maybe a better query or promises. Maybe read a bit more about async JavaScript here to get a better understanding.
当然有更多的解决方案,例如更好的查询或承诺。也许在这里阅读更多关于异步JavaScript的内容以获得更好的理解。