首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有查询参数的节点js - app.get方法.内部服务器错误

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

Stack Overflow用户
提问于 2014-10-10 02:52:32
回答 2查看 746关注 0票数 0

Controller.js

代码语言:javascript
复制
  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

代码语言:javascript
复制
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.中的路由

代码语言:javascript
复制
ApiRouter.get('/query',auth.requiresLogin, ApiCtrl.load);

下面是我的节点js方法来处理get.

代码语言:javascript
复制
exports.load = function(req, res) {
    console.log("req query");
    console.log(req.query);
    var myList=[];
     return res.status(200)
                .send(myList);
};

我得到了以下错误。

代码语言:javascript
复制
http://localhost:3000/api/item/query?page=0&perPage=3  500 (Internal Server Error)

误差跟踪

代码语言:javascript
复制
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写作的机会。请让我知道我哪里错了。

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-10 03:08:03

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

该路由的处理程序试图使用该路由param查询Mongo,可能使用findById或类似的方式传递id,该id在此时具有' query‘值。

票数 1
EN

Stack Overflow用户

发布于 2014-10-10 03:08:38

您的代码块正在使用“_id”的“查询”在数据库中查找项。这听起来不符合我的常识。

代码语言:javascript
复制
loadItems: function(paginate) {
                return restAngular.all('query').**getList(paginate)**;
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26291214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档