call - 收益调用返回未定义
问题描述
任何人都可以帮助我的代码有什么问题。我试图从这样的 sagas 文件中的 API 获取数据。我尝试在读取函数中控制台记录 response.data 并获得数据(数组)。但是当它在生成器函数的变量(数据)中赋值时,它没有定义。
// sagas.js
const PATH = 'product';
const read = async (path) => {
await request.get(path)
.then(response => {
console.log('response from read :', response.data)
return response.data;
})
.catch(err => { throw err })
}
function* loadProduct() {
try {
const data = yield call(read, PATH);
yield put(actions.loadProductSuccess(data));
}
catch (error) {
console.log('what's the error :', error)
yield put(actions.loadProductFail());
}
}
export default function* rootSaga() {
yield all([
takeEvery('LOAD_PRODUCT', loadProduct)
]);
}
解决方案
非常感谢,我的问题解决了。我忘记了花括号,我像这样更改我的代码:
const PATH = 'product';
const read = async (path) =>
await request.get(path)
.then(response => {
console.log('response from read :', response.data)
return response.data;
})
.catch(err => { throw err })
...
它工作正常!
推荐阅读
- testing - HELM 3:Helm 测试不起作用?获得“测试套件:无”
- python - 使用 CDK 中的 CfnFunction 引用 SAM 中的本地代码和 Python
- python - 尝试检索 .jpg EXIF 数据时不断返回“找不到文件”
- r - R中的多重回归
- mongodb - 在 Mongodb 中使用 $trunc 或 $round 不保证 2 位小数
- javascript - 在 textarea 光标旁边设置 Bootstrap Popover 位置
- c++ - 传递从嵌入式资源文件创建的 ifstream 以增强 XML 解析器
- python-3.x - 如何同时运行收集和协程任务
- c++ - dbg 无法使用 WSL2 处理 VScode - 无法解析不存在的文件“vscode-remote://wsl+ubuntu-20.04..”
- java - 使用 Java 传输客户端连接到弹性搜索时出现“不支持 TLSv1.3”错误