2016-08-30 3 views
2

Первый кусок как создатель действия ниже работает как с thunk, но я также хочу применить второй кусок, который является промежуточным программным обеспечением. Как настроить его, чтобы он мог отправить 2 действия?реакция промежуточного программного обеспечения relx 2 действия

export const fetchPokemon = function (pokemonName) { 
    return function (dispatch) { 
    dispatch({type: 'REQUESTING'}) 
    const requestURL = `http://pokeapi.co/api/v2/pokemon/${pokemonName}/` 
    return fetch(requestURL) 
    .then(function (response) { 
     return response.json() 
    }) 
    .then(function (data) { 
     dispatch(receivePokemon(formatPokemonData(data))) 
     dispatch(fetchPokemonDescription(pokemonName)) 
    }) 
    } 
} 

промежуточного

const fetchPromiseMiddleware = store => next => action => { 
    if (typeof action.then !== 'function') { 
    return next(action) 
    } 
    return Promise.resolve(action).then(function (res) { 
    if (res.status >= 400) { 
     throw new Error("Bad response from server") 
    } 
    return res.json() 
    }).then(store.dispatch) 
} 

Я пробовал ниже, но получаю сообщение об ошибке:

store.js:33 Uncaught (in promise) TypeError: (0 , _actionCreators.receivePokemon) is not a function

const fetchPromiseMiddleware = store => next => action => { 
    if (typeof action.then !== 'function') { 
    return next(action) 
    } 
    return Promise.resolve(action).then(function (res) { 
    if (res.status >= 400) { 
     throw new Error("Bad response from server") 
    } 
    return res.json() 
    }).then(function (data) { 
    return store.dispatch(receivePokemon(formatPokemonData(data))) 
    }).then(function (data) { 
    return store.dispatch(fetchPokemonDescription(data.name)) 
    }) 
} 
+0

Можете ли вы показать остальную часть своих действий? –

ответ

0

там не хватает код в ваш вопрос, но мне кажется, когда вы звоните receivePokemon(formatPokemonData(data)) в коде, который вы показываете, receivePokemon не является функцией, теперь вам нужно проверить, где это определено, возможно, это нет.

 Смежные вопросы

  • Нет связанных вопросов^_^