【问题标题】:.then() on node modules not working?.then() 在节点模块上不起作用?
【发布时间】:2017-10-29 23:56:50
【问题描述】:

我现在正在开发一个 react native 应用程序,我编写了自己的模块来存储一些与我的组件分开的信息。

当我这样做时,我注意到在我创建的模块中,我有一个这样的函数:

testFetch = function(url){
return fetch(url);
}

exports.test = testFetch;

在我的主模块中,我执行了以下操作:

import ts from 'test-module'

ts.test("http://example.com").then((response) => {console.log(response)});

但是 .then() 并没有因为任何原因被解雇。请求通过并成功。

有什么帮助吗?

【问题讨论】:

  • 请确认示例中的错误命名并非来自现实生活(请更正)。
  • 添加一个catch 处理程序。你可能有一个错误,因为没有catch,它被忽略了。
  • exports.test = testFetch 中,您没有显示testFetch 的定义位置,然后在导入它时,您尝试使用ts.testFetch() 而不是ts.test()。您对名称的命名和使用都搞砸了。
  • 好吧,我的错,抱歉我更正了。当然问题仍然存在并且不会改变任何东西
  • 即使在您编辑之后,您仍然调用ts.testFetch(),但您导出的符号是ts.test()。请注意写出准确的问题。

标签: javascript node.js reactjs react-native module


【解决方案1】:

问题在于您混合 CommonJS 和 ES6 模块的方式。您似乎期望示例主模块中的 ts 为您提供模块依赖项中整个 export 的值,但这不是它的工作原理。

您可以使用export default 导出您的testFetch 函数,如下所示:

testFetch = function (url) {
    return fetch(url);
}

export default testFetch;

主模块:

import testFetch from 'test-module';

testFetch("http://example.com").then((response) => {console.log(response)});

【讨论】:

    猜你喜欢
    • 2022-08-24
    • 2019-03-24
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多