[英]Node js - app.get method with query params - internal server error
Controller.js 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 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. 以下是我在节点js中的路线。
ApiRouter.get('/query',auth.requiresLogin, ApiCtrl.load);
The below is my node js method to handle the get. 下面是我的节点js方法来处理获取。
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.
如果我将路由器中的“ getList”更改为“ post(paginate)”,则不会收到此错误。 Some where I am missing out to write for getList.
我错过了一些为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. 网址
/api/item/query
在执行基于ID的查询的路由之一上匹配。 I'm assuming it's something like a '/item/:id'
or the like. 我假设它类似于
'/item/:id'
之类的东西。
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'. 该路由的处理程序正在尝试使用该路由参数来查询Mongo,可能使用
findById
或类似的方法,并传递ID,此时其值为“ query”。
Your code block is looking for an item in the database with the _id of "query". 您的代码块正在数据库中寻找_id为“ query”的项目。 That doesnt sound right to me based on common sense.
基于常识,这对我来说听起来不对。
loadItems: function(paginate) {
return restAngular.all('query').**getList(paginate)**;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.