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

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

本文关键字:方法 内部 服务器 错误 get js-app 查询 参数 节点      更新时间:2023-09-26

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.

下面是我在nodejs中的路由

ApiRouter.get('/query',auth.requiresLogin, ApiCtrl.load);

下面是我的node-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(paginate)",则不会出现此错误。有些地方我错过了为getList写东西。请告诉我哪里错了。

谢谢。

看起来它在另一个控制器操作上匹配。url /api/item/query在您的一条正在进行基于ID的查询的路由上匹配。我假设它是'/item/:id'之类的。

该路由的处理程序正试图使用该路由参数来查询Mongo,可能使用findById或类似的方法,并传递此时具有值"query"的id。

您的代码块正在数据库中查找id为"query"的项。基于常识,这听起来不对。

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