【发布时间】:2019-03-25 20:14:06
【问题描述】:
我编写了一个 API 包装器,它使用 fetch 来执行 API 请求。因为我希望该模块与浏览器、Node 和 React Native 兼容,所以我使用cross-fetch。现在,如果我用 Node 测试它,它可以完美运行,但是如果我在 React Native 中使用包装器,如果失败并出现以下错误:TypeError: undefined is not a function (evaluating 'cross_fetch_1.default(url, init)')。如果我打印 cross_fetch_1 的类型,我会得到:
{ [Function]
polyfill: true,
Response: { [Function: Response] error: [Function], redirect: [Function] },
Request: [Function: Request],
Headers: [Function: Headers] }
cross_fetch_1.default 或 cross_fetch_1.fetch 的“未定义”。
该库是用 Typescript 开发的,因此 cross_fetch_1 变量由 TS 编译器创建。你可以在这里https://github.com/tgamauf/onetimesecret-api/blob/1b8edff66bde11807c8ff7d29030b4d3e6661277/lib/request.ts#L150找到Typescript代码,在这里https://github.com/tgamauf/onetimesecret-api/blob/1b8edff66bde11807c8ff7d29030b4d3e6661277/lib/request.js#L179找到编译好的JS。
据我所知,我正在使用应该使用的模块。这是什么原因造成的?
我还在这里打开了一个关于此的 github 问题:https://github.com/lquixada/cross-fetch/issues/26
【问题讨论】:
-
如果您要同时提交有关同一问题的 Stack Overflow 问题和问题报告,请相互链接以帮助防止重复工作。
-
你说得对,我已经添加了问题的链接!
标签: javascript typescript react-native fetch-api