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.
The below is my route in node js.
ApiRouter.get('/query',auth.requiresLogin, ApiCtrl.load);
The below is my node js method to handle the get.
exports.load = function(req, res) {
console.log("req query");
console.log(req.query);
var myList=[];
return res.status(200)
.send(myList);
};
I get the below error.
http://localhost:3000/api/item/query?page=0&perPage=3 500 (Internal Server Error)
Error trace
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
I am getting Internal server error for no reason. If I change the 'getList' in my router to 'post(paginate)' then I dont get this error. Some where I am missing out to write for getList. Please let me know where I am wrong.
Thanks.
Looks like it's matching on another controller action. The url /api/item/query
is matching on one of your routes that is doing an ID-based query. I'm assuming it's something like a '/item/:id'
or the like.
The handler for that route is trying to use that route param to query Mongo, probably using a findById
or similar and passing the id which at this point has the value 'query'.
Your code block is looking for an item in the database with the _id of "query". That doesnt sound right to me based on common sense.
loadItems: function(paginate) {
return restAngular.all('query').**getList(paginate)**;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.