【问题标题】:Uncaught TypeError: Cannot read property 'type' of undefined React Router Redux未捕获的类型错误:无法读取未定义的 React Router Redux 的属性“类型”
【发布时间】:2018-02-02 22:24:11
【问题描述】:

使用 "react-router-dom": "^4.1.2", "react-router-redux": "^5.0.0-alpha.6" 我得到标题为 "Uncaught TypeError: Cannot read property '重定向完成后,console.log() 中未定义的 React Router Redux 类型。 屏幕截图显示包含有效负载数据的可观察操作不是问题。有效负载包含表单数据(值)和历史对象。 我设法发送它们,然后通过使用 mergeMap 而不是 ES6 映射的递归函数来捕获它们,因为我正在处理 Observables。 link to the repo

    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);}
                            );
            });
    };

【问题讨论】:

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


    【解决方案1】:

    在 createPostEpic 中,您将 ajax 调用的结果映射为空(未定义),因为您的动作创建者调用和返回被注释掉,因此您的史诗最终发出值 undefined

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

    undefined 值将被分派,redux-observable 首先将其传递给下一个中间件(或根 reducer)。 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) 功能。使用它,我几乎可以立即找到原因。一项非常有益的技能,让我的生活变得如此轻松。

    【讨论】:

    • 谢谢@jayphelps,我很感激。
    猜你喜欢
    • 2017-04-25
    • 2017-04-13
    • 1970-01-01
    • 2018-01-05
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多