首页 > 解决方案 > 使用 fetch() 时继续得到“未处理的承诺拒绝:TypeError:网络请求失败”

问题描述

我正在尝试从项目中的文本文件中获取数据并将其打印出来作为测试,但我仍然遇到同样的错误。

下面是我的代码:

import React from 'react';
import { Stylesheet, View, Text, Button } from 'react-native';
import { beginQuiz } from './helperfunctions';
import myText from './sampleFile.js';

export const QuestionScreen = ({ navigation, route }) => {
    fetch(myText)
                .then(response => response.text())
                .then(data => console.log(data))
                .catch(function(error) {
                  console.log('There has been an error');
                  throw error;
                })
                console.log('done');
    return (
        <View>
            <Text>This is the question screen</Text>
            <Button title='Sample Button'/>
        </View>
    )
  };

这是 sampleFile.js:

var myText = 'This is a sample text file.'

标签: javascript

解决方案


推荐阅读