0

如果我忘记包含任何内容,首先道歉,这里是新的!我在使用 react-native-windows 版本 0.63.0-0 和 react-native 版本 0.63.2 时遇到了一个非常奇怪的问题。问题是从我的 Windows 笔记本电脑进行的网络调用看起来好像它们没有尝试执行...期间。它们会立即失败并简单地返回一个 TypeError。

TypeError: Network request failed

这发生在我全新的 RN/RNW 初始化中,并且我已经使用几个 http 库和调用的变体进行了测试,例如使用相同的 then 和 catch 逻辑交换 axios 以获取所有内容(在下面的一些示例中除外为简洁起见)。我尝试过但未能到达几个不同的域。我尝试访问的 API 可用(通过邮递员成功测试)。其他标准的 React-Native 项目在我的笔记本电脑上工作得很好。我已经清除了 Metro,运行 --reset-cache,删除了 tmp,重新安装了节点模块,所有你应该尝试像这样进行故障排除的标准。

fetch('https://subdomain.domain.com/<api-path>', { // my example then & catch
      method: "GET",
      headers: { "Content-Type": "application/json" }
    })
      .then(response => response.json())
      .then(responseData => console.log(responseData))
      .catch(error => console.log(error));
fetch('https://subdomain.domain.com/<api-path>'); // without headers this time, used same then & catch block
axios.get('https://subdomain.domain.com/<api-path>'); // again used same then & catch
// ... and so on, triggering the catch block instantly, as if it fails without trying

我还在调试器控制台中看到以下错误,即使我可以从我的“chrome dev tools -> sources -> page”面板中看到 debuggerWorker,据我了解,该错误仅与 chrome 不处理有关源映射正确,请随时纠正我。但是我不确定它们是否相关,或者这只是一个侥幸:

Error: Unable to resolve module `./debugger-ui/debuggerWorker.cff11639.js` from ``: ./debugger-ui/debuggerWorker.cff11639.js could not be found within the project.

If you are sure the module exists, try these steps:
 1. Clear watchman watches: watchman watch-del-all
 2. Delete node_modules: rm -rf node_modules and run yarn install
 3. Reset Metro's cache: yarn start --reset-cache
 4. Remove the cache: rm -rf /tmp/metro-*
    at ModuleResolver.resolveDependency (C:\Users\...\node_modules\metro\src\node-haste\DependencyGraph\ModuleResolution.js:186:15)
    at ResolutionRequest.resolveDependency (C:\Users\...\node_modules\metro\src\node-haste\DependencyGraph\ResolutionRequest.js:52:18)
    at DependencyGraph.resolveDependency (C:\Users\...\node_modules\metro\src\node-haste\DependencyGraph.js:287:16)
    at C:\Users\...\node_modules\metro\src\lib\transformHelpers.js:267:42
    at Server.<anonymous> (C:\Users\...\node_modules\metro\src\Server.js:841:41)
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (C:\Users\...\node_modules\metro\src\Server.js:99:24)
    at _next (C:\Users\...\node_modules\metro\src\Server.js:119:9)

通常我会运行npx react-native run-windows(或npx react-native start --reset-cache先单独运行)来执行项目,但是从 VS Code 的调试窗格进行调试以及从 Visual Studio 运行会产生相同的错误。

我的反应原生 Windows 设置是否缺少一些东西?我已经按照 Windows 设置一步一步进行了几次,甚至只是简单地添加一个 http 请求以从他们的默认应用程序执行立即失败......这已经阻止了我一段时间,它真的感觉像是一个环境设置问题,但我不知道我还会在 Visual Studio/我的配置中检查什么来解决这个问题。

4

1 回答 1

0

在对这个问题进行了一些更深入的思考之后,我继续深入挖掘了 Visual Studio 的一面。我逐个文件查找与可能阻止此功能的远程相关的任何内容。正如我所料,这是一个不幸的简单解决方案,因为我没有意识到它并且无法在其他任何地方找到它,所以让我非常头疼-本机-Windows:

每当您开始 - 或拿起 - 使用网络发送或检索信息的 RNW 项目时,请确保您首先进入 Visual Studio(不是 VScode)并启用“私有网络(客户端和服务器)”选项。如果你在 Visual Studio 中打开你的项目,你可以找到它,它位于

Project Solution -> 'Package.appxmanifest' file -> 'Capabilities' section

从现在开始,这应该有望使您的网络请求成功。

于 2020-09-30T23:22:58.220 回答