我正在尝试将卡片和剩余的东西从获取中分发到store(redux),但是我遇到了这个错误,它使函数无法进一步执行。
这是我尝试删除的剩余部分和deck_id的方法,它可以工作,但是当我包括剩余部分时,它会停止
export const fetchDrawCard = deck_id => dispatch =>{
return fetch(`${API_ADDRESS}/deck/${deck_id}/draw`)
.then(response => {
if(response.status != 200){
throw new Error('Unsuccesfull Requrest to deckofcardsapi.com')
}
return response.json();
// console.log(response.json());
})
.then(json => {
console.log(json);
console.log(json.cards);
console.log(json.remaining);
dispatch({
type: DECK_DRAW.FETCH_SUCCESS,
cards:json.cards,
remaining: json.remaining
}) ;
console.log('Fetch Success');
})
.catch( error=>dispatch({ type: DECK_DRAW.FETCH_ERROR, message: error.message}))
let DEFAULT_DECK = { deck_id: '', remaining: 0, fetchState: '', message: '', cards: [], };
const deckReducer = (state = DEFAULT_DECK, action) => {
let cards, remaining;
switch(action.type){
case DECK.FETCH_SUCCESS:
const {remaining, deck_id} = action;
return{ ...state, remaining, deck_id, fetchState : fetchStates.success};
case DECK.FETCH_ERROR:
return { ...state, message: action.message, fetchState : fetchStates.error};
case DECK_DRAW.FETCH_SUCCESS:
// ( { cards, remaining} = action);
remaining = action.remaining;
deck_id = action.deck_id;
// cards = action.cards;
// console.log(action.remaining);
// console.log(deck_id);
// console.log(remaining);
return { ...state,cards, remaining, fetchState: fetchStates.success};
// return { ...state, cards, remaining, fetchState: fetchStates.success};
case DECK_DRAW.FETCH_ERROR:
console.log('Fetch Error');
return { ...state, cards, remaining, fetchState: fetchStates.error};
default :
return state ;
};
它应该只记录调度成功本身,但它会记录成功,然后在调度功能中进一步停止错误