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

TypeError: res.json不是函数——Bug追踪器接口返回数据异常排查

问题分析与解决方案

核心问题

前端fetch调用未添加await,导致res接收的是Promise对象而非Response实例,自然不存在json()方法,触发TypeError: res.json is not a function。

修复代码

只需要给fetch调用添加await,获取真正的响应对象即可:

const saveBug = async function () {
const dataArray = [...new FormData(form)];
const bug = Object.fromEntries(dataArray);
bug.createdBy = activeUser.user.username;

try {
  // 关键:给fetch添加await,获取真正的Response对象
  const res = await fetch('', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Accept: 'application/json',
      Authorization: `Bearer ${activeUser.token}`,
    },
    body: JSON.stringify({
      bug: bug,
    }),
  });
  const data = await res.json();
  console.log(data);

  document.querySelector('.bug-form').reset();

  fetchBugs();
} catch (err) {
  console.log(err);
}
};

额外排查建议

  • 确认前端fetch的URL是否正确(当前为空字符串,需指向后端实际接口路径)
  • 后端可在res.status(201).send(bug)前添加console.log(bug),验证返回的数据结构是否符合预期
  • 检查Bug模型是否配置了toJSON方法,确保MongoDB文档能被正确序列化为JSON格式返回

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 09:25:28