首页 文章

路由名称错误的MongooseJS奇怪的投射错误

提问于
浏览
1

我正在后端工作,我有这条路线应该从数据库返回一个ID数组,以便以后延迟加载 . 我的路线定义如下:

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....]

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

对于模型“内部人员”,路径“_id”处的值“list”的转换为ObjectId失败CastError:对于新的CastError(C:\ Users \)模型“内部人员”的路径“_id”处的值“list”,转换为ObjectId失败在ObjectId.cast上的rutherfordc \ Documents \ GitHub \ ccs-express-mongo \ node_modules \ mongoose \ lib \ error \ cast.js:27:11)(C:\ Users \ rutherfordc \ Documents \ GitHub \ ccs-express-mongo \在ObjectId.SchemaType.applySetters(C:\ Users \ rutherfordc \ Documents \ GitHub \ ccs-express-mongo \ node_modules \ mongoose \ lib \ schematype.js)中的node_modules \ mongoose \ lib \ schema \ objectid.js:158:13): 724:12)在ObjectId.SchemaType.castForQuery(C)的ObjectId.SchemaType._castForQuery(C:\ Users \ rutherfordc \ Documents \ GitHub \ ccs-express-mongo \ node_modules \ mongoose \ lib \ schematype.js:1113:15) :\ Users \ rutherfordc \ Documents \ GitHub \ ccs-express-mongo \ node_modules \ mongoose \ lib \ schematype.js:1103:15)at ObjectId.SchemaType.castForQueryWrapper(C:\ Users \ rutherfordc \ Documents \ GitHub \ ccs- show-mongo \ node_modules \ mongoose \ lib \ schematype.js:1082:15)演员表(C:\用户\ rutherfordc \ Documents \ GitHub \ ccs-express-mongo \ node_modules \ mongoose \ lib \ cast.js:300:32)在model.Query.Query.cast(C:\ Users \ rutherfordc \ Documents \ GitHub \ ccs- express-mongo \ node_modules \ mongoose \ lib \ query.js:3309:12)at model.Query.Query._castConditions(C:\ Users \ rutherfordc \ Documents \ GitHub \ ccs-express-mongo \ node_modules \ mongoose \ lib \ query.js:1293:10)在进程中的model.Query.Query._findOne(C:\ Users \ rutherfordc \ Documents \ GitHub \ ccs-express-mongo \ node_modules \ mongoose \ lib \ query.js:1518:8) .nextTick(C:\ Users \ rutherfordc \ Documents \ GitHub \ ccs-express-mongo \ node_modules \ kareem \ index.js:333:33)处于_combinedTickCallback(internal / process / next_tick.js:131:7) . _tickCallback(internal / process / next_tick.js:180:9)

我已经确认它实际上是泄漏到我的路由处理程序中的路由名称 . (即我将路线改为 /jerry 为S&G,"list"在错误中被"jerry"替换)

2 回答

  • 0

    我认为问题出在你的 find 方法中,试着把它改成这样

    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

    原来这是一个路线优先问题 . 在 /list 路线之前有一条 /:insiderId 路线 .

    道歉 .

相关问题