【发布时间】:2018-01-10 10:13:01
【问题描述】:
我对使用 thunk getState 有点陌生,我什至一直在尝试 console.log 方法并什么也没得到。在状态下,我看到 loginReducer 具有我需要进行 API 调用的关键属性。 status(pin): true
key(pin): "Ls1d0QUIM-r6q1Nb1UsYvSzRoaOrABDdWojgZnDaQyM"
这里我有一个服务:
import axios from 'axios'
import {thunk, getState} from 'redux-thunk'
import MapConfig from '../components/map/map-config'
const origin = 'https://us.k.com/'
class KService {
getNorthAmericaTimes() {
return (dispatch, getState) => {
const key = getState().key
console.log('This is time key,', key)
if (key) {
dispatch(axios.get(`${origin}k51/api/datasets/k51_northamerica?key=${key}`))
}
}
// const url = `${origin}k51/api/datasets/k51_northamerica?key=${urlKey}`
// return axios.get(url)
}
}
export default new K51Service()
但是在我的相应操作中,我得到了Uncaught TypeError: _kService2.default.getNorthAmericaTimes(...).then is not a function
这是动作函数的样子:
export function getKNorthAmericaTime(dispatch) {
KService.getNorthAmericaTimes().then((response) => {
const northAmericaTimes = response.data[0]
dispatch({
type: ActionTypes.SET_NORTH_AMERICA_TIMES,
northAmericaTimes
})
})
}
我假设它可能与 if 块没有被执行有关。
【问题讨论】:
标签: reactjs axios redux-thunk