简体   繁体   English

提取为空Redux Saga

[英]Fetch is empty Redux Saga

I just started learning Redux Saga 5 mins ago 我刚刚在5分钟前开始学习Redux Saga

My saga: 我的传奇:

import axios from "axios";
import {call, put, takeLatest} from "redux-saga/effects";
import {
    USERS_FETCH_REQUESTED,
    USERS_FETCH_SUCCEEDED,
    USERS_FETCH_FAILED
} from 'actions/constants'

function *fetchUsers(action) {
  try {
    const response = yield call(fetch, 'https://jsonplaceholder.typicode.com/users');
    const data = response.json();
    console.log(data)
    yield put({type: USERS_FETCH_SUCCEEDED, payload: data});
  } catch (e) {
    yield put({type: USERS_FETCH_FAILED, payload: e.message});
  }
}

function* mySaga() {
    yield takeLatest(USERS_FETCH_REQUESTED, fetchUsers);
}

export default mySaga

for some reason my SUCCES_FETCH's payload is just an empty object, BUT the log give back 10 10 items. 由于某种原因,我的SUCCES_FETCH的有效负载只是一个空对象,但是日志却返回了10 10个项目。

What am I doing wrong? 我究竟做错了什么?

response.json() returns a promise, you need to wait for the promise to resolve to get to the items. response.json()返回一个Promise,您需要等待Promise解析后才能到达项目。

const data = yield call([response, 'json']);

This will produce an effect which calls response.json() and feeds the results back into the saga after the promise resolves. 这将产生一个效果,该效果调用response.json()并在承诺解决后将结果反馈到传奇中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM