node.js - 无法获取我的设置 404 状态代码,而是为不正确的 ID 获取 500 状态代码
问题描述
我是 Nodejs 的新手,我正在尝试使用 node js 来玩这些东西。
我创建了一个 api(使用 Nodejs、Express、Mongoose)来读取我试图搜索无效用户 id 的用户(id 与 mongoDB 中的任何记录都不匹配)。
404 响应永远不会触发。我输入了错误的 ID 并得到 500。
附上代码截图供参考:
解决方案
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
推荐阅读
- vb.net - “未指定输入源”
- python - 带有 GUI 的手写数字识别
- mapping - 我们是否能够从机器人车队经理那里提取地图信息?
- visual-studio - 启用空安全时,默认“列表”构造函数不可用
- sql - 如何在 presto 中首先运行子查询
- python - 如果响应状态不是 200 并且它处于并发期货会话中,我将如何循环 HTTP 请求?Python
- php - PHP 替换倍数 \r\n 为
- hibernate - Hibernate 的 session.save 如何代理一个瞬态对象?
- flutter - 将 'obscureText' 标志设置为 true 会使文本字段在它们没有聚焦时清晰 - 为什么?
- odoo - 如何在odoo v10中将“day, hh:mm:ss”格式转换为hh:mm