简体   繁体   English

React Native Windows 项目中的网络调用立即失败

[英]Network calls failing instantly in React Native Windows project

first off apologies if I've forgotten to include anything, new here!如果我忘记包含任何内容,首先道歉,新来的! I am having a pretty odd issue using react-native-windows version 0.63.0-0 with react-native version 0.63.2.我在使用 react-native-windows 版本 0.63.0-0 和 react-native 版本 0.63.2 时遇到了一个非常奇怪的问题。 The problem is that network calls made from my windows laptop appear as if they are not attempting to execute... period.问题是从我的 Windows 笔记本电脑发出的网络调用似乎没有尝试执行...期间。 They fail instantaneously and simply return a TypeError.它们会立即失败并简单地返回一个 TypeError。

TypeError: Network request failed

This happens to me on a completely fresh init of RN/RNW, and I've tested with a couple http libraries and variations of calls, such as swapping in axios for fetch all using the same then and catch logic (excluded on some examples below for brevity's sake).这发生在我全新的 RN/RNW init 上,我已经测试了几个 http 库和调用的变体,例如交换 axios 以使用相同的 then 和 catch 逻辑来获取所有内容(在下面的一些示例中除外为简洁起见)。 I have tried and failed to reach several different domains.我曾尝试但未能到达几个不同的域。 The API I'm attempting to reach is available (tested successfully with postman).我尝试访问的 API 可用(已通过邮递员成功测试)。 Other standard React-Native projects work just fine from my laptop.其他标准的 React-Native 项目在我的笔记本电脑上工作得很好。 I've cleared metro, run --reset-cache, removed tmp, reinstalled node-modules, everything standard you're supposed to try to troubleshoot like that.我已经清除了地铁,运行 --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

I do also see the following error in my debugger console, even though I can see the debuggerWorker from within my 'chrome dev tools -> sources -> page' panel which from what I understand, the error just has to do with chrome not handling source maps correctly, feel free to correct me there.我也确实在调试器控制台中看到了以下错误,即使我可以从我的“chrome 开发工具 -> 源 -> 页面”面板中看到 debuggerWorker,据我所知,该错误仅与 chrome 不处理有关源映射正确,请随时纠正我。 However I'm unsure whether they are even related or if this one is just a fluke:但是,我不确定它们是否相关,或者这只是侥幸:

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)

Generally I'll run npx react-native run-windows (or npx react-native start --reset-cache separately first) to execute the project, but debugging from VS Code's debug pane, as well as running from Visual Studio produce the same errors.通常我会运行npx react-native run-windows (或先分别npx react-native run-windows npx react-native start --reset-cache )来执行项目,但是从 VS Code 的调试窗格进行调试,以及从 Visual Studio 运行会产生相同的结果错误。

Is there something I am missing with my react native windows setup?我的 React Native Windows 设置有什么遗漏吗? I have followed the windows setup step by step several times and even simply adding an http request to execute from their default app instantly fails... This has blocked me for a while and it really feels like an environment setup issue, but I'm not sure what else I would check in Visual Studio/my configs in order to resolve this.我已经一步一步地遵循 windows 设置几次,甚至只是简单地添加一个 http 请求从他们的默认应用程序执行立即失败......这已经阻止了我一段时间,它真的感觉像是一个环境设置问题,但我是不知道我还会在 Visual Studio/我的配置中检查什么来解决这个问题。

After some more continuously banging my head against this issue, I continued to dig a little further into Visual Studio's side.在对这个问题不断地敲打我的头之后,我继续深入挖掘 Visual Studio 的一面。 I went file by file looking for anything remotely related to what might be blocking this functionality.我逐个文件查找与可能阻止此功能的远程相关的任何内容。 As I expected, it was an unfortunately simple solution to something that caused me a significant headache since I wasn't aware of it and could not find it anywhere else, so let this be a lesson to those after me who have network problems with React-Native-Windows:正如我所料,这是一个不幸的简单解决方案,让我非常头疼,因为我不知道它并且在其他任何地方都找不到它,所以让这对我之后遇到 React 网络问题的人来说是一个教训-本机-Windows:

Whenever you are beginning - or picking up - a RNW project that uses the network to send or retrieve information, make sure that you have first gone into Visual Studio (not VScode) and enabled the "Private Networks (Client & Server)" option.每当您开始或选择使用网络发送或检索信息的 RNW 项目时,请确保您已首先进入 Visual Studio(而非 VScode)并启用“私有网络(客户端和服务器)”选项。 You can find this if you open your project in Visual Studio, and it's under如果您在 Visual Studio 中打开您的项目,您可以找到它,它位于

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

This should hopefully cause your network requests to succeed as you would expect from now on.从现在开始,这有望使您的网络请求成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gradle / Gradlew一直在React Native项目中失败 - Gradle/Gradlew keeps failing in a React Native project 世博项目的本机链接失败 - react-native link failing with expo project android React Native项目的StripeModule失败 - StripeModule failing for android React Native project 如何使用oauth 1.0与react native进行网络通话? - how to make network calls with react native using oauth 1.0? React Native项目初始化错误(Windows) - React Native project init error (windows) React Native API 调用 .NET 核心后端仅在启用 React Native 调试器网络检查时工作 - React Native API Calls to .NET Core backend only work when React Native Debugger Network Inspect Enabled 由于gradle问题,React Native演示项目失败 - React Native demo project is failing due to gradle issue Fresh React Native 项目失败:条目,“:CFBundleIdentifier”,不存在 - Fresh React Native project failing: Entry, ":CFBundleIdentifier", Does Not Exist 无法构建和运行 React Native 项目。 Pod 安装失败 - Unable to build and run react native project. Pods failing to install 在 Typescript React 原生项目中 DrawerNavigator 的 Jest 测试失败 - Jest test failing for DrawerNavigator in a Typescript React native project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM