You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

req.params.id末尾含多余空格引发ObjectId转换错误求助

问题:请求ID时触发CastError错误

错误详情

const castError = new CastError();
stringValue: '"6317861ce9acfa3f2d2101a3 "',
kind: 'ObjectId',
value: '6317861ce9acfa3f2d2101a3 ',
path: '_id',

我确定错误根源是ID末尾存在多余空格,但不清楚空格的来源。如果需要更多代码排查方案,请说明。

关联代码

router.put('/:id', async (req, res, next) => {
    req.article = await Article.findById(req.params.id);
    next();
}, saveArticleAndRedirect('edit'))

内容的提问来源于stack exchange,提问作者Noah Little

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 23:31:00