首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MongooseJS奇数转换错误,路由名称错误

MongooseJS奇数转换错误,路由名称错误
EN

Stack Overflow用户
提问于 2018-06-06 03:39:21
回答 2查看 78关注 0票数 1

我在一个后端上工作,我有一个路由,它应该只从数据库返回一个ID数组,以便稍后延迟加载。我的路由定义如下:

代码语言:javascript
复制
 router.get('/list', (req, res) => {
   Insider.find({}, {_id}).then(insiders => {
     if (!insiders) {
       res.status(400).json({ error: 'unable to find list of insiders' });
     }
     res.json(insiders);
   }).catch(err => res.status(400).json(err));
 });

它应该返回如下所示的[_id, _id, _id....]数组

但是我得到了一个非常奇怪的错误:

(C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules的新ObjectId (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules中,对模型"insiders“CastError的路径"_id”处的值"list“进行CastError强制转换到ObjectId失败:对_id处的模型"insiders”在路径"_id“处的值"list”转换为CastError失败\mongoose\lib\schema\objectid.js:158:13)在ObjectId.SchemaType.applySetters (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1113:15) at ObjectId.SchemaType.castForQuery的ObjectId.SchemaType.applySetters Users (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1103:15)在model.Query的cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\cast.js:300:32) at ObjectId.SchemaType.castForQueryWrapper (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\mongoose\lib\schematype.js:1082:15).Query.cast (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\在model.Query.Query._castConditions (C:\Users\rutherfordc\Documents\GitHub\ccs-express-mongo\node_modules\的.Query.cast model.Query.Query._castConditionsmongoose\lib\query.js:1518:8)在_combinedTickCallback (内部/进程/next_tick.js:131:7)的process.nextTick进程,在process._tickCallback (内部/进程/next_tick.js:180:9)

我已经确认它实际上是泄漏到我的路由处理程序中的路由名称。(例如,我将S&G的路由更改为/jerry,错误中的"list“替换为"jerry”)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-06 04:00:16

我认为问题出在您的find方法中,尝试将其更改为如下所示

代码语言:javascript
复制
 router.get('/list', (req, res) => {
 Insider.find({}, {id:0}).then(insiders => {
  if (!insiders) {
   res.status(400).json({ error: 'unable to find list of insiders' });
   }
     res.json(insiders);
  }).catch(err => res.status(400).json(err));
});

我认为您应该使用id而不是_id,并将其值设置为0

票数 0
EN

Stack Overflow用户

发布于 2018-06-07 01:06:15

事实证明这是一个路线优先级问题。在/list路由之前有一个/:insiderId路由。

很抱歉。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50707748

复制
相关文章

相似问题

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