我正在尝试生成一个响应,该响应返回按3个不同列排序的相同集合.这是我目前的代码: var findRoute = router.route("/find")findRoute.get(function(req, res) { Box.find(function(err, boxes) { res.json(boxes) }).so
var findRoute = router.route("/find")
findRoute.get(function(req, res) {
Box.find(function(err, boxes) {
res.json(boxes)
}).sort("-itemCount");
});
正如您所看到的,我们正在制作单个get请求,查询Boxes,然后在最后按itemCount对它们进行排序.这对我不起作用,因为请求只返回按itemCount排序的单个JSON集合.
如果我想返回另外两个按名称和大小属性排序的集合,我可以做什么?所有这些都在同一个请求中?
克里特岛一个对象来封装信息并链接你的查找查询,如:var findRoute = router.route("/find");
var json = {};
findRoute.get(function(req, res) {
Box.find(function(err, boxes) {
json.boxes = boxes;
Collection2.find(function (error, coll2) {
json.coll2 = coll2;
Collection3.find(function (error, coll3) {
json.coll3 = coll3;
res.json(json);
}).sort("-size");
}).sort("-name");
}).sort("-itemCount");
});
只需确保进行适当的错误检查.
这有点像uggly,使你的代码难以阅读.尝试使用像async或甚至承诺这样的模块来适应这种逻辑(Q和bluebird就是很好的例子).
