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

Netlify无服务器函数访问/:uid端点报n is not a function错误求解决

问题原因

你遇到的报错是node-fetch导入不兼容和返回值处理错误共同导致的:

  1. 目前node-fetch v3及以上版本仅支持ESM规范导入,你使用CommonJS的require语法引入时,拿到的不是fetch函数本身,而是一个模块对象。调用该对象时就会抛出「不是函数」的错误,代码打包后变量被混淆为n,所以报错信息显示n is not a function。因为根路由/没有触发fetch相关逻辑,所以运行正常,只有访问/:uid调用fetch时才会报错。
  2. 就算fetch引入正确,node-fetch执行后返回的是Response实例,不能直接通过res.json返回给前端,需要先调用.json()方法解析响应体内容。

修复方案

  1. 修复node-fetch引入问题,二选一即可:
    • 降级到支持CommonJS的v2版本,执行命令:npm install node-fetch@2
    • 若要保留v3版本,将引入代码改为:const fetch = require("node-fetch").default
  2. 修改/:uid路由的响应处理逻辑,正确解析返回数据:
router.get("/:uid", async (req, res) => {
  try {
    const response = await fetchWithToken(`/${req.params.uid}`, "GET");
    // 新增响应解析步骤
    const data = await response.json();
    res.status(200).json(data);
  } catch (error) {
    console.log(error.message);
    res.status(500).json({ error: error.message });
  }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 11:18:03