首页 > 解决方案 > 无法获取我的设置 404 状态代码,而是为不正确的 ID 获取 500 状态代码

问题描述

我是 Nodejs 的新手,我正在尝试使用 node js 来玩这些东西。

我创建了一个 api(使用 Nodejs、Express、Mongoose)来读取我试图搜索无效用户 id 的用户(id 与 mongoDB 中的任何记录都不匹配)。

404 响应永远不会触发。我输入了错误的 ID 并得到 500。

附上代码截图供参考:

在此处输入图像描述

标签: node.jsmongodbexpressmongoose

解决方案


for using findById you should pass only the id as a single value not in an object. so update your code as

User.findById(_id) 

and you are good to go


推荐阅读