我有一个我的博览会应用程序正在查询的节点后端。node-express-mongo 后端工作得非常完美,我可以使用 Postman 的 GET 请求进行验证,但我的应用程序中出现未处理的承诺拒绝网络失败错误
完全错误:
[Unhandled promise rejection: TypeError: Network request failed]
- node_modules/whatwg-fetch/dist/fetch.umd.js:473:29 in xhr.onerror
- node_modules/event-target-shim/dist/event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:574:29 in setReadyState
- node_modules/react-native/Libraries/Network/XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js:190:12 in emit
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:436:47 in __callFunction
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:111:26 in __guard$argument_0
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:384:10 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
api.js
export const fetchMeetups = () =>
fetch('http://localhost:3000/api/meetups')
.then(res => res.json());
Run Code Online (Sandbox Code Playgroud)
应用程序.js
import * as React from 'react';
import { Platform, StyleSheet, Text, View, …Run Code Online (Sandbox Code Playgroud) 我第一次尝试阿波罗。我的后端服务器是 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));
Run Code Online (Sandbox Code Playgroud)
但我有一个错误。
[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: …Run Code Online (Sandbox Code Playgroud)