嗨,我如何读取从获取调用返回的数据:
export function* fetchMessages(channel) {
yield put(requestMessages())
const channel_name = channel.payload
try {
const response = yield call(fetch,'/api/messages/'+channel_name)
const res = response.json()
console.log(res)
yield put(receiveMessages(res,channel))
} catch (error){
yield put(rejectMessages(error))
}
}
当我使用 console.log(res) 我得到:
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
__proto__
:
Promise
[[PromiseStatus]]
:
"resolved"
[[PromiseValue]]
:
Array[7]
我如何从这个 promise 中获取我的“信息”(Array[7] 端?我是新手。谢谢
最佳答案
response.json() 是异步的并返回 promise
改变这个
const res = response.json()
到
const res = yield response.json()
关于javascript - Redux-Saga 无法正确读取来自 fetch api 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39201374/