【问题标题】:Why is API request returning [object Object] in the reducer's action value?为什么 API 请求在 reducer 的操作值中返回 [object Object]?
【发布时间】:2018-05-26 01:52:07
【问题描述】:

我正在尝试使用 Giphy API、redux 和 axios 制作一个搜索应用程序,但我认为我在请求从 API 获取所有搜索结果时出错了。

我使用一个动作发出 API 请求,该动作被减速器捕获,但是当我 console.log 记录减速器内的动作值时,我得到的是 [object Object] 而不是实际的对象。为什么是这样?

我使用 ReduxPromise 作为我的中间件。

这是我在操作代码中的 API 请求:

import axios from 'axios';

export const FETCH_GIPHS = 'FETCH_GIPHS'

export function fetchGiphs(value) {
    const api = "http://api.giphy.com/v1/gifs/search";
    const API_KEY = 'hdUk5buTTISSC29bx2DAXfDRCz6tkcrS';

    const url = `${api}?q=${value}&api_key=${API_KEY}&limit=5"`;

    //http://api.giphy.com/v1/gifs/search?q=rainbow&api_key=hdUk5buTTISSC29bx2DAXfDRCz6tkcrS&limit=5"

    const request = axios.get(url);
    console.log('Request:', request)

    return {
        type: FETCH_GIPHS,
        payload: request
    }
}

和减速器:

export default function(state = null, action) {
    console.log('action recieved: ' + action)
return state;
} 

还有我的主要 index.js,我的中间件在哪里

const createStoreWithMiddleware = applyMiddleware(ReduxPromise)(createStore);

ReactDOM.render(
    <Provider store={createStoreWithMiddleware(reducers)}>
        <App />
    </Provider>
, document.getElementById('root'));

【问题讨论】:

标签: reactjs redux react-redux axios redux-promise


【解决方案1】:

我认为你的问题是你没有调度你的行动

this.dispatch(this.loadGiphs(0, 3)); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 2011-07-13
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多