Невозможно разрешить "выборку" - PullRequest
0 голосов
/ 24 марта 2020

Я пытаюсь запустить выставочный проект, (реактивный), но когда я запускаю проект, я получаю следующий результат:

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

Выборка не должна быть nodejs стандартной функцией? Что я должен сделать, чтобы запустить fetch с проектом?

Кроме того, если я пытаюсь yarn add fetch, я получаю другую ошибку.

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:

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

1 Ответ

1 голос
/ 24 марта 2020

Согласно Expo do c вам не нужно ничего импортировать. Просто используйте fetch в коде.

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 (
      ....
    );
  }
}
...