【问题标题】:[Network error]: TypeError: Network request failed[网络错误]:TypeError:网络请求失败
【发布时间】:2020-04-09 05:41:49
【问题描述】:

我是第一次尝试阿波罗。 我的后端服务器是 Phoenix 框架(长生不老药)。并在http://localhost:4000/api 中运行 所以我尝试在我的代码中使用这样的 apollo 进行第一次查询。

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));

但我遇到了一个错误。

[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

在我的服务器日志中什么也没说。我认为我的客户甚至没有接触服务器。 我错过了什么?有任何想法吗? :(

【问题讨论】:

    标签: react-native elixir phoenix-framework apollo absinthe


    【解决方案1】:

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

    localhost 指向代码运行的环境。模拟器模拟真实设备,而模拟器仅模拟设备。 因此,Android 上的 localhost 指向模拟的 Android 设备。而不是运行服务器的机器。 解决方法是将 localhost 替换为您机器的 IP 地址。

    尝试将http://localhost:4000 更改为http://10.0.2.2:4000/

    【讨论】:

    猜你喜欢
    • 2018-06-11
    • 2022-01-12
    • 2018-01-09
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    相关资源
    最近更新 更多