我的Redux是生锈的,至少可以说,我正在尝试从返回Promise的API导入函数。我想将这个Promise添加到action creator的有效负载中,但它抱怨TypeError: Cannot read property 'getBetList' of undefined
这是我的动作创建者:
import { getBetList, getLatestPrices } from '../api/index';
export const GET_BET_LIST = 'GET_BET_LIST';
export const GET_LATEST_PRICES = 'GET_LATEST_PRICES';
export function fetchBetList() {
const response = this.getBetList();
return {
type: GET_BET_LIST,
payload: response
}
}
export function fetchLatestPrices() {
const response = this.getLatestPrices();
return {
type: GET_LATEST_PRICES,
payload: response
}
}
我假设从API收到的Promise将在有效负载上可用,将流经redux-promise,并最终获得我需要的数据,但它停止并返回错误,它无法读取getBetList?在这里,我在导入时做错了什么?
发布于 2018-07-29 04:46:14
您应该直接使用getBetList
& getLatestPrices
,因为您是在文件顶部导入它的。从下面的行中删除this
将修复您的错误。
const response = this.getBetList();
^^^^
这里也是一样
const response = this.getLatestPrices();
顺便说一句,它也不会工作,你应该使用异步流程,就像这样:
export function fetchLatestPrices() {
return (dispatch) => {
getLatestPrices()
.then(response => dispatch({type: GET_LATEST_PRICES, payload: response}));
}
}
有关async actions
的更多信息
https://stackoverflow.com/questions/51575095
复制相似问题