axios:无法读取null

时间:2016-10-28 09:00:28

标签: javascript node.js axios

使用redux进行反应服务器端渲染时出现此错误

堆栈跟踪

 TypeError: Cannot read property 'slice' of null
    at dispatchHttpRequest (E:\Projects\real-estate\node_modules\axios\lib\adapters\http.js:84:37)
    at httpAdapter (E:\Projects\real-estate\node_modules\axios\lib\adapters\http.js:19:10)
    at dispatchRequest (E:\Projects\real-estate\node_modules\axios\lib\core\dispatchRequest.js:52:10)
    at process._tickCallback (internal/process/next_tick.js:103:7)

动作

import axios from 'axios';

export const FETCH_USER = 'FETCH_USER';
export const FETCH_POSTS = 'FETCH_POSTS';
const GET_POSTS_URI = '/api/posts';

export const fetchPosts = () => {
        let posts = axios.get(GET_POSTS_URI).catch(err => console.log(err));
        return {
            type: FETCH_POSTS,
            payload: posts
        }
}

API

app.get('/api/posts', function(req, res, next) {
    Post.model.find()
        .exec()
        .then(function(posts) {
            res.send(posts);
        }, function(err) {
            return next(err);
        });
})

我仍然在开发工具的网络选项卡中获得了JSON响应,但我无法在reducer中检索JSON响应。当我使用OpenWeatherAPI

时,我也没有收到此错误

1 个答案:

答案 0 :(得分:0)

你缺少redux-thunk,因为你需要它来做副作用(比如api调用)。看看这个:redux-thunkredux async actions