所以我正在尝试学习react-redux-saga。我正在构建一个简单的应用程序,该应用程序仅从API抓取用户数据并显示它。这是我的传奇中的生成器函数:
export function* fetchImage() {
try {
const response = yield call(fetch, 'https://randomuser.me/api/');
console.log("Response 111", response);
const responseBody = response.json();
yield put(setImage(response));
} catch (e) {
yield put(fetchFailed(e));
}
return;
}
它只是对URL进行yield调用,然后调度一个动作。但是,响应对象不会以JSON的形式返回,而是看起来像返回了一些响应对象:
我不确定该对象如何处理。如果您直接通过浏览器访问URL,它将返回一个JSON对象。如何获取返回JSON的信息?
所以我的response.json()返回此对象:
看起来像我想要的对象在[[PromiseValue]]中。那是什么,我如何实现我的价值?
答案 0 :(得分:1)
解析后,您始终可以访问响应对象的主体:
const response = yield call(fetch, 'https://randomuser.me/api/');
const responseBody = yield response.json(); // HERE is what you want
答案 1 :(得分:0)
通过使用call
方法,这是完成相同工作的另一种方式。
const response = yield call(fetch, 'https://randomuser.me/api/');
const body = yield call([response, 'json']);
console.log(body)
另一种方式
const body = yield call([response, response.json]);