Uncaught TypeError: Cannot read property 'type' of undefined React Router Redux
Uncaught TypeError: Cannot read property 'type' of undefined React Router Redux
使用 "react-router-dom": "^4.1.2", "react-router-redux": "^5.0.0-alpha.6" 我按照 "Uncaught TypeError: Cannot read property 'type' of undefined React Router Redux" 在 console.log() 重定向完成后的那一刻。
屏幕截图显示包含有效负载数据的可观察操作不是问题。有效负载包含表单数据(值)和历史对象。
我设法发送它们,然后通过使用 mergeMap 而不是 ES6 map 的递归函数捕获它们,因为我正在处理 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);}
);
});
};
在 createPostEpic 中,你将 ajax 调用的结果映射为空(未定义),因为你的动作创建者调用和 return 被注释掉了,所以你的史诗最终发出值 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) 的能力。使用它,我几乎可以立即找到原因。一项非常有益的技能,让我的生活变得如此轻松。
使用 "react-router-dom": "^4.1.2", "react-router-redux": "^5.0.0-alpha.6" 我按照 "Uncaught TypeError: Cannot read property 'type' of undefined React Router Redux" 在 console.log() 重定向完成后的那一刻。 屏幕截图显示包含有效负载数据的可观察操作不是问题。有效负载包含表单数据(值)和历史对象。 我设法发送它们,然后通过使用 mergeMap 而不是 ES6 map 的递归函数捕获它们,因为我正在处理 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);}
);
});
};
在 createPostEpic 中,你将 ajax 调用的结果映射为空(未定义),因为你的动作创建者调用和 return 被注释掉了,所以你的史诗最终发出值 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) 的能力。使用它,我几乎可以立即找到原因。一项非常有益的技能,让我的生活变得如此轻松。