在第一个yield调用()之前,React-Native fetch()等于false

时间:2019-01-29 16:40:07

标签: react-native fetch redux-saga saga stanza.io

问题:

我想将stanza.io添加到我的本机项目中。经过研究,我跟随this安装了stanza.io。它似乎工作得很好,但是甚至在到达新的节代码之前就出现了问题:我有一个来自redux-sagasaga,其任务是通过redux-触发对fetch()的两个同时调用。传奇的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;

软件版本:

  • 本机:0.57.1​​
  • 反应:16.5.0

0 个答案:

没有答案