如何从 react-redux 中的 Promise 对象获取普通 JSON 对象?
我的 action.js 包含:
export function allEmp() {
let url = "employees?access_token=";
let result = ApiCall.getApiCall(url)
.then(function (response) {
return response;
})
.catch(function (error) {
return error;
});
console.log("result",result);
return {
type: "ALL_EMP",
payload: new Promise((resolve, reject) => {
setTimeout(() => {
resolve(result);
}, 2000);
})
};
}
我的 API 调用配置是:
getApiCall(url) {
let base_url = "http://192.168.1.151:3000/api/";
let api_token = "1f7169e92c1d0722db575b877707cf0b88b8f0ad";
let fetch_url = base_url + url + api_token;
let myHeaders = new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json'
});
return fetch(fetch_url, {
method: "GET",
headers: myHeaders
})
.then(function(response) {
if (response.ok) {
return response.json();
} else {
var error = new Error(response.statusText);
error.response = response;
throw error;
}
})
.then(function(json) {
return json;
})
}
我的 store.js :
import {createStore, combineReducers, applyMiddleware} from "redux";
import thunk from "redux-thunk";
import promise from "redux-promise-middleware";
import userDetails from "./reducers/User_reducer";
export default createStore(
combineReducers({
userDetails
}),
{},
applyMiddleware(thunk, promise())
);
但是在这里我得到了 Promise 对象。因此,当我在有效负载中分配时,它变得未定义。当我将响应作为有效负载发送时,我应该如何操作它。
谁能给我一些建议?