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

Palantir Foundry:TypeScript后端函数发起HTTP请求捕获JSON响应失败求助

解决Palantir Foundry中TypeScript函数HTTP请求编译失败问题

问题原因

你碰到的Module parse failed错误,是因为node-fetch-native包使用了Palantir Foundry Functions编译环境不兼容的语法或模块格式。实际上Foundry Functions运行时已经内置了标准的Fetch API,完全不需要额外安装第三方fetch库。

修复步骤

1. 移除第三方fetch依赖

直接删除代码里的import fetch from "node-fetch-native";语句,Foundry Functions支持直接使用全局的fetch方法。

2. 补全未定义的类型

代码中的MyExpectedData类型没有声明,必须补充类型定义(建议根据实际响应结构编写),或者临时用any替代:

// 示例:根据你的API响应结构定义类型
interface MyExpectedData {
  id: number;
  title: string;
  // 其他字段按需添加
}

3. 配置网络访问权限

确保你的Foundry函数已经获得访问目标URL的权限:

  • 进入Foundry控制台,找到对应的函数项目
  • 在「网络配置」或「权限」面板中,将目标URL添加到允许访问的列表中

修复后的完整代码

import { OntologyEditFunction } from "@foundry/functions-api";

// 定义响应数据的类型结构
interface MyExpectedData {
  // 替换为你的实际响应字段
  [key: string]: any;
}

export class MyFunctions {
    @OntologyEditFunction()
    async fetchTestData(url: string): Promise<void> {
        try {
            const response = await fetch(url);
            if (!response.ok) {
                throw new Error(`HTTP error! Status: ${response.status}`);
            }
            const data: MyExpectedData = await response.json();
            console.log(data);
        } catch (error) {
            console.error('Error fetching data:', error);
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 19:42:22