未捕获的TypeError:无法读取未定义的React Router Redux的属性“type”

时间:2017-08-24 22:16:43

标签: reactjs redux react-router-redux redux-observable

使用“react-router-dom”:“^ 4.1.2”,“react-router-redux”:“^ 5.0.0-alpha.6”我按标题获取“Uncaught TypeError:无法读取属性”在重定向完成后的一刻,在console.log()中键入'undefined React Router Redux'。 屏幕截图显示包含有效负载数据的动作observable不是问题。 Payload包含表单数据(值)和历史对象。 我设法发送它们然后通过使用mergeMap而不是ES6 map的递归函数捕获它们,因为我正在处理Observables。 link to the repo

enter image description here

    import * as ActionTypes from "../ActionTypes";
    import { createPostFulfilled, fetchPosts, fetchPostsFulfilled, fetchPostsWithIdFulfilled, changeRoute } from "../actions";
    import {store, history} from "../configureStore";
    import { Observable } from "rxjs/Observable";
    import "rxjs/add/observable/combineLatest";
    import "rxjs/add/operator/debounceTime";
    import { concat as concat$ } from "rxjs/observable/concat";
    import { from as from$ } from "rxjs/observable/from";
    import { of as of$ } from "rxjs/observable/of";
    import "rxjs/add/operator/map";
    import "rxjs/add/operator/mergeMap";
    import "rxjs/add/operator/startWith";
    import "rxjs/add/operator/filter";
    import "rxjs/add/operator/switchMap";
    import "rxjs/add/operator/concatMap";
    import "rxjs/add/operator/catch";
    import "rxjs/add/observable/dom/ajax";
    import {push} from "react-router-redux";
    const ROOT_URL = "http://reduxblog.herokuapp.com/api";
    const API_KEY = "key=davide123";

    export  const fetchPostsEpic = (action$) => {

        return action$.filter((action$)=> action$.type === ActionTypes.FETCH_POSTS)
            .mergeMap(action$ => {
              return  Observable.ajax.getJSON(`${ROOT_URL}/posts/?${API_KEY}`)
                    .map(response => fetchPostsFulfilled(response), (err) => {console.log(err);});
            });
    };

    export const fetchPostsWithIdEpic = (action$) => {
        return action$.filter((action$)=> action$.type === ActionTypes.FETCH_POSTS_WITH_ID)
        .mergeMap(action$ => {
          return  Observable.ajax.getJSON(`${ROOT_URL}/posts/?${action$.payload}&${API_KEY}`)
                .map(response => fetchPostsWithIdFulfilled(response), (err) => {console.log(err);});
        });
    };

    export  const createPostEpic = (action$) => {
        const actionVectorizer = ((action$) => {
            if (action$)
            return action$.isArray() ?   [].concat(...action$.mergeMap(x => actionVectorizer(x))) : action$;
        })();
        return action$.filter((action$)=> action$.type === ActionTypes.CREATE_POST)
            .mergeMap(action$ => {
                    console.log("action$ is...");
                    console.log(action$);
                    let { values, history } = action$.payload;
                    return   Observable.ajax.post(`${ROOT_URL}/posts/?${API_KEY}`, values)
                            .map(
                                (data) => {
                                    if (data.status === 201) {
                                        console.log("Success status", data.status);
                                        history.push("/");
                                        // return createPostFulfilled(data.status);
                                    }
                                    else {console.log("Server error is", data.status);}
                                },
                                (err) => {console.log(err);}
                            );
            });
    };

1 个答案:

答案 0 :(得分:1)

在createPostEpic中,你将ajax调用的结果映射为nothing(undefined),因为你的动作创建者调用和返回是注释掉的,所以你的史诗最终会发出值undefined

history.push("/");
// return createPostFulfilled(data.status);

将调度undefined值,并且redux-observable首先将其传递给下一个中间件(或根减速器)。接下来是In your case, react-router-redux middleware,因此它会收到它。

但是,react-router-redux只需要真正的操作,因此错误是因为it tries to look up action.type但是操作未定义。

我认为您真的很喜欢了解Chrome强大的调试工具,例如Pause on Uncaught Exceptions (or even Caught ones)的功能。使用它我几乎可以立即找到原因。一项非常有益的技巧让我的生活变得如此轻松。