我在一个后端上工作,我有一个路由,它应该只从数据库返回一个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....]
数组
但是我得到了一个非常奇怪的错误:
在
(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”)
发布于 2018-06-06 04:00:16
我认为问题出在您的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
发布于 2018-06-07 01:06:15
事实证明这是一个路线优先级问题。在/list
路由之前有一个/:insiderId
路由。
很抱歉。
https://stackoverflow.com/questions/50707748
复制相似问题