【发布时间】:2016-05-10 17:15:47
【问题描述】:
我遇到了一个问题,我通过页面控制器从后端调用数据库查询,如果该函数没有在后端控制器中立即返回,那么页面控制器中会出现错误。我不知道哪一方是罪魁祸首:angular 还是 nodejs?
仅供参考,当我手动输入后端路由时,如果查询非常非常小并且几乎立即返回,则页面控制器中的功能是成功的。
页面控制器
$scope.getItem = function(item) {
Item.item_query({ itemId: itemId}, function(result) {
console.log("result" + result);
}, function(err) {
console.log("err" + err);
});
};
资源服务
window.app.factory("Item", function ($resource) {
return $resource('api/:action/:itemId',{},
{
get_item_files: {
method: 'GET',
isArray: true,
params: {
action: 'items'
}
},
item_query: {
method: 'GET',
isArray: true,
params: {
action: 'items',
itemId: '@itemId'
},
cache: false
}
});
});
后端控制器
exports.getItem = function(req, res) {
var itemId = req.params.itemId;
var query = "SELECT c.name, * FROM (\
SELECT p.champion_id\
, count(p.item0 = ? OR NULL)::int2 AS it0\
, count(p.item1 = ? OR NULL)::int2 AS it1\
, count(p.item2 = ? OR NULL)::int2 AS it2\
, count(p.item3 = ? OR NULL)::int2 AS it3\
, count(p.item4 = ? OR NULL)::int2 AS it4\
, count(p.item5 = ? OR NULL)::int2 AS it5\
FROM matchversion mv\
CROSS JOIN matchtype mt\
JOIN match m USING (matchtype_id, matchversion_id)\
JOIN participant p USING (match_id)\
WHERE mv.matchversion = \'5.14\'\
AND mt.matchtype = \'RANKED_SOLO_5x5\'\
AND p.winner = True\
GROUP BY p.champion_id\
HAVING count(p.item0 = ? OR NULL)::int2 > 0\
OR count(p.item1 = ? OR NULL)::int2 > 0\
OR count(p.item2 = ? OR NULL)::int2 > 0\
OR count(p.item3 = ? OR NULL)::int2 > 0\
OR count(p.item4 = ? OR NULL)::int2 > 0\
OR count(p.item5 = ? OR NULL)::int2 > 0\
) p\
JOIN champion c USING (champion_id)";
var bindings = [itemId, itemId, itemId, itemId, itemId, itemId, itemId,
itemId,itemId,itemId,itemId,itemId]
return pg.raw(query, bindings).then(function(resp) {
return resp.rows;
}).then(function(rows) {
res.jsonp(rows);
}).catch(function(err){
console.log(err);
});
}
后端路由
var items = require('../app/controllers/itemBackend');
app.get('/api/items/:itemId', items.getItem);
【问题讨论】:
-
打开你的开发工具,看看请求是否到达了后端。错误代码是什么?
-
有一个请求,带有一些临时标头。它说它无法加载响应数据。
-
什么是状态码?
-
没有状态码。刚刚又试了一下,发现是“被角度取消了”。
-
可能是 CORS 问题。控制台中是否打印了任何错误?
标签: angularjs node.js postgresql