React-Native fetch () равно false перед первым вызовом yield () - PullRequest
0 голосов
/ 29 января 2019

Проблема:

Я хочу добавить stanza.io к моему реактивному проекту.После некоторых исследований я последовал this , чтобы установить stanza.io.Кажется, он работает довольно хорошо, но проблема возникает еще до того, как я достиг нового кода строфы: у меня есть saga из redux-saga, задача которого - инициировать 2 одновременных вызова на fetch() через redux-саги yield call(), такие как

const res = yield call(fetch, fetchUrl, { method: 'GET', headers });

Только первый yield call(fetch) завершается с ошибкой uncaught at check call: argument false is not a function.Поэтому я решил console.log(fetch) перед каждым yield call(fetch) и обнаружил, что перед первым вызовом fetch равен false, это действительно не функция.Но для каждого последующего вызова fetch прекрасно работает и имеет правильный прототип, теперь он снова является функцией.

Мои выборки запускаются следующим образом:

const [result1, result2] = yield all([
      call(fetch1, option),
      call(fetch2, option),
    ]);

Вот весь файл:

import { actionChannel, call, take, put, select, all } from 'redux-saga/effects';
import { device, sanitize } from 'utils';
import config from 'config';
import idx from 'idx';
import moment from 'moment';
import { TradSingleton } from 'trads';
import { LocaleConfig } from 'react-native-calendars';

function* fetchWebTrads(language) {
  try {
    const fetchUrl = `https://example.com/some_route/${language}`;
    const headers = {
      'Content-Type': 'application/json',
    };
    const res = yield call(fetch, fetchUrl, { method: 'GET', headers });
    const response = yield res.json();
    return { trad: response };
  } catch (e) {
    console.log('Error', e);
    return { trad: null, error: e };
  }
}

function* fetchMobileTrads(language) {
  try {
    const fetchUrl = `https://example.com/another_route/${language}`;
    const headers = {
      'Content-Type': 'application/json',
    };
    const res = yield call(fetch, fetchUrl, { method: 'GET', headers });
    const response = yield res.json();
    return { trad: response };
  } catch (e) {
    console.log('Error', e);
    return { trad: null, error: e };
  }
}

function* fetchTrads() {
  try {
    moment.locale('en');
    const language = device.getTradLanguage();
    const [mobileTrads, webTrads] = yield all([
      call(fetchMobileTrads, language),
      call(fetchWebTrads, language),
    ]);
    if (mobileTrads.trad && webTrads.trad) {
      moment.locale(language);
      try {
        LocaleConfig.locales[language] = {
          monthNames: moment.months(),
          monthNamesShort: moment.monthsShort(),
          dayNames: moment.weekdays(),
          dayNamesShort: moment.weekdaysShort(),
        };

        LocaleConfig.defaultLocale = language;
      } catch (e) {
        console.log('Agenda locals fail', e);
      }
      TradSingleton.setLocale(language);
      TradSingleton.setTrads([mobileTrads.trad, webTrads.trad]);
      yield put({ type: 'FETCH_TRADS_SUCCESS' });
    } else {
      yield put({
        type: 'FETCH_TRADS_FAILURE',
        error: !mobileTrads.trad ? mobileTrads.error : webTrads.error,
      });
    }
  } catch (e) {
    console.log('Error', e);
    yield put({ type: 'FETCH_TRADS_FAILURE', error: e });
  }
}

function* watchFetchTrads() {
  const requestChan = yield actionChannel('FETCH_TRADS');
  while (true) {
    yield take(requestChan);
    yield call(fetchTrads);
  }
}

export default watchFetchTrads;

Версии программного обеспечения:

  • React-Native: 0.57.1 ​​
  • React: 16.5.0
...