繁体   English   中英

节点js-具有查询参数的app.get方法-内部服务器错误

[英]Node js - app.get method with query params - internal server error

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方法来处理获取。

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(paginate)”,则不会收到此错误。 我错过了一些为getList编写的地方。 请让我知道我错了。

谢谢。

看起来与另一个控制器动作匹配。 网址/api/item/query在执行基于ID的查询的路由之一上匹配。 我假设它类似于'/item/:id'之类的东西。

该路由的处理程序正在尝试使用该路由参数来查询Mongo,可能使用findById或类似的方法,并传递ID,此时其值为“ query”。

您的代码块正在数据库中寻找_id为“ query”的项目。 基于常识,这对我来说听起来不对。

loadItems: function(paginate) {
                return restAngular.all('query').**getList(paginate)**;
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM