Controller.js
function _loadMoreItems() {
console.log("load more items");
var paginate = {
page : $scope.page,
perPage : $scope.perPage
};
ItemService.loadItems(paginate).then(function(result){
console.log("pagination done");
console.log("result");
console.log(result);
console.log("result length");
console.log(result.length);
for(var i=0;i<result.length;i++){
$scope.itemList.push(result[i]);
}
$scope.page = $scope.page + 1;
},function(err){
console.log("error");
console.log(err);
});
}Service.js - ItemService
angular.module("myapp.item")
.factory("myapp.item.service", ['Restangular',
function(Restangular) {
var restAngular = Restangular.withConfig(function(Configurer) {
Configurer.setBaseUrl('/api/item');
});
return {
create: restAngular.all('')
.post,
loadItems: function(paginate) {
return restAngular.all('query').getList(paginate);
}
}
}
]);
Here I pass the paginate object as query param.下面是我在节点js.中的路由
ApiRouter.get('/query',auth.requiresLogin, ApiCtrl.load);下面是我的节点js方法来处理get.
exports.load = function(req, res) {
console.log("req query");
console.log(req.query);
var myList=[];
return res.status(200)
.send(myList);
};我得到了以下错误。
http://localhost:3000/api/item/query?page=0&perPage=3 500 (Internal Server Error)误差跟踪
CastError: Cast to ObjectId failed for value "query" at path "_id"
at ObjectId.cast (D:\Branching\8OctItemPagination\myapp.io\node_modules\mongoose\lib\schema\ob
jectid.js:116:13)
at ObjectId.castForQuery (D:\Branching\8OctItemPagination\myapp.io\node_modules\mongoose\lib\s
chema\objectid.js:165:17)
at Query.cast (D:\Branching\8OctItemPagination\myapp.io\node_modules\mongoose\lib\query.js:233
6:32)
at Query.findOne (D:\Branching\8OctItemPagination\myapp.io\node_modules\mongoose\lib\query.js:
1118:10)
at Query.exec (D:\Branching\8OctItemPagination\myapp.io\node_modules\mongoose\node_modules\mqu
ery\lib\mquery.js:2213:16)
at Query.exec (D:\Branching\8OctItemPagination\myapp.io\node_modules\mongoose\lib\query.js:179
7:19)
at exports.itemid (D:\Branching\8OctItemPagination\myapp.io\app\item\server\controllers\api\item-api.js:36:10)
at paramCallback (D:\Branching\8OctItemPagination\myapp.io\node_modules\express\lib\router\ind
ex.js:385:7)
at param (D:\Branching\8OctItemPagination\myapp.io\node_modules\express\lib\router\index.js:36
5:5)
at Function.proto.process_params (D:\Branching\8OctItemPagination\myapp.io\node_modules\expres
s\lib\router\index.js:391:3)
info: GET /api/item/query?page=0&perPage=3 500 24.740 ms - 0我得到内部服务器错误没有任何理由。如果我将路由器中的'getList‘改为’post(分页)‘,那么我就不会得到这个错误。有些地方我错过了为getList写作的机会。请让我知道我哪里错了。
谢谢。
发布于 2014-10-10 03:08:03
看起来它在另一个控制器动作上匹配。url /api/item/query在执行基于ID的查询的路由上进行匹配。我假设它类似于'/item/:id'之类的东西。
该路由的处理程序试图使用该路由param查询Mongo,可能使用findById或类似的方式传递id,该id在此时具有' query‘值。
发布于 2014-10-10 03:08:38
您的代码块正在使用“_id”的“查询”在数据库中查找项。这听起来不符合我的常识。
loadItems: function(paginate) {
return restAngular.all('query').**getList(paginate)**;
}https://stackoverflow.com/questions/26291214
复制相似问题