无法解析“获取"消息. [英] expo Unable to resolve "fetch"

查看:72
本文介绍了无法解析“获取"消息.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个expo项目,(本机),但是当我运行该项目时,我得到以下结果:

I am trying to run an expo project, (react-native) but when i run the project, i get following result:

Unable to resolve "fetch" from "src\components\MyScreen\Screen.js"

获取不应该是Node.js的标准功能吗?我应该怎么做才能对该项目进行抓取?

Fetch should not be a nodejs standard feature? What should i do to run fetch with the project?

此外,如果我尝试yarn add fetch,则会收到其他错误.

Also, if i try to yarn add fetch, i get a different error.

The package at "node_modules\fetch\lib\fetch.js" attempted to import the Node standard library module "http". It failed because React Native does not include the Node standard library. Read more at https://docs.expo.io/versions/latest/introduction/faq/#can-i-use-nodejs-packages-with-expo

我想念什么吗?

这是即时通讯调用fetch的地方:

Here is where im calling fetch:

fetch(`${API_URL()}/orders`, requestInfo)
  .then(response => {
    if (response.ok) {
      return response.json();
    } else {
      throw new Error("Erro ao registrar endereço!");
    }
  })
  .then(currentUser => {
    this.setState({ loading: false });
    alert("Pedido realizado com sucesso!");
    this.props.navigation.navigate("Products");
  })
  .catch(error => {
    this.setState({ loading: false });
    alert(error.message);
  });

推荐答案

根据 Expo文档,您无需导入任何内容.只需在代码中使用fetch.

According to Expo doc you don't need to import anything. Just use fetch in the code.

import React from 'react';
import { FlatList, ActivityIndicator, Text, View } from 'react-native';

export default class FetchExample extends React.Component {
  constructor(props) {
    super(props);
    this.state = { isLoading: true };
  }

  componentDidMount() {
    return fetch('https://facebook.github.io/react-native/movies.json')
      .then(response => response.json())
      .then(responseJson => {
        this.setState(
          {
            isLoading: false,
            dataSource: responseJson.movies,
          },
          function() {}
        );
      })
      .catch(error => {
        console.error(error);
      });
  }

  render() {
    return (
      ....
    );
  }
}

这篇关于无法解析“获取"消息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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