[网络错误]:TypeError:网络请求失败 [英] [Network error]: TypeError: Network request failed

查看:245
本文介绍了[网络错误]:TypeError:网络请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次尝试阿波罗.我的后端服务器是Phoenix框架(elixir).并在 http://localhost:4000/api 中运行因此,我尝试在代码中使用像这样的apollo进行第一个查询.

I am trying apollo for the first time. My backend server is Phoenix framework(elixir). and running in http://localhost:4000/api So I tried to do a first query using apollo like this in my code.

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import ApolloClient from 'apollo-boost';
import { gql } from 'apollo-boost';
const client = new ApolloClient(
  {
    uri: 'http://localhost:4000/api',
  }
);
client.query({
   query: gql`
     { 
        users {
          name
          email
        } 
     `}
}).then(result => console.log(result));

但是我遇到了一个错误.

But I got an error.

[Network error]: TypeError: Network request failed

    node_modules/expo/build/logs/LogSerialization.js:166:14 in _captureConsoleStackTrace
    node_modules/expo/build/logs/LogSerialization.js:41:24 in serializeLogDataAsync
    ... 9 more stack frames from framework internals

[Unhandled promise rejection: Error: Network error: Network request failed]

    http://192.168.1.8:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false:124432:28 in new ApolloError
    node_modules/apollo-client/bundle.umd.js:1587:12 in <anonymous>
    node_modules/apollo-client/bundle.umd.js:2007:12 in info.listeners.forEach$argument_0
    <anonymous>:null in Set.forEach
    node_modules/apollo-client/bundle.umd.js:2005:17 in queries.forEach$argument_0
    <anonymous>:null in Map.forEach
    node_modules/apollo-client/bundle.umd.js:2003:11 in QueryManager.prototype.broadcastQueries
    http://192.168.1.8:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false:125823:31 in <unknown>
    node_modules/promise/setimmediate/core.js:37:11 in tryCallOne
    node_modules/promise/setimmediate/core.js:123:14 in setImmediate$argument_0

在我的服务器日志中什么也没说.我认为我的客户甚至都没有碰过服务器.我想念什么?有任何想法吗?:(

And in my server log said nothing. I think my client doesn't even touch the server. What am I missing? any ideas? :(

推荐答案

问题是,iOS在模拟器中运行,而Android在模拟器中运行.

The thing is, that iOS is running in a simulator and Android is running in an emulator.

localhost指向运行代码的环境.当模拟器仅模仿设备时,模拟器将模拟真实设备.因此,Android上的localhost指向仿真的Android设备.而不是运行服务器的计算机.解决方案是用计算机的IP地址替换localhost.

The localhost is pointing to the environment in which the code is running. The emulator emulates a real device while the simulator is only imitating the device. Therefore the localhost on Android is pointing to the emulated Android device. And not to the machine on which your server is running. The solution is to replace localhost with the IP address of your machine.

尝试通过 http://10.0.2.2:4000/

这篇关于[网络错误]:TypeError:网络请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆