无法读取未定义的 属性 'then'
Cannot read property 'then' of undefined
所以我正在使用 react + redux,但我继续收到以下错误:"Cannot read property 'then' of undefined"。由于某种原因,承诺没有被退回。我对使用 redux thunk 也特别陌生。
减速机
import { merge } from 'lodash';
import * as APIutil from '../util/articles_api_util';
import {
ArticleConstants
} from '../actions/article_actions';
const ArticlesReducer = (state = {}, action) => {
switch (action.type) {
case ArticleConstants.RECEIVE_ALL_ARTICLES:
debugger
return merge({}, action.articles);
default:
return state;
}
};
export default ArticlesReducer;
商店
import { createStore, applyMiddleware } from 'redux';
import RootReducer from '../reducers/root_reducer';
import thunk from 'redux-thunk';
import * as APIUtil from '../util/articles_api_util';
export const ArticleConstants = {
RECEIVE_ALL_ARTICLES: "RECEIVE_ALL_ARTICLES",
REQUEST_ALL_ARTICLES: "REQUEST_ALL_ARTICLES"
}
操作数
export function fetchArticles() {
return function(dispatch) {
return APIUtil.fetchArticles().then(articles => {
dispatch(receiveAllArticles(articles));
}).catch(error => {
throw(error);
});
};
}
export const requestAllArticles= () => ({
type: REQUEST_ALL_ARTICLES
});
export const receiveAllArticles = articles => ({
type: RECEIVE_ALL_ARTICLES,
articles
});
const configureStore = (preloadedState = {}) => (
createStore(
RootReducer,
preloadedState,
applyMiddleware(thunk)
)
);
export default configureStore;
APIUtil
export const fetchArticles = (success) => {
$.ajax({
method: 'GET',
url: `/api/articles`,
success,
error: ()=> (
console.log("Invalid Article")
)
});
};
箭头函数只做隐含的 return
s 如果你离开大括号。一旦包含花括号,就定义了一个函数体,需要显式 return
一个值。
您的 fetchArticles
函数被写成带有花括号的箭头函数。但是,您并未明确 returning $.ajax()
调用的结果。因此,该函数的 return 值为 undefined
,并且没有承诺 returned 可以链接。
所以我正在使用 react + redux,但我继续收到以下错误:"Cannot read property 'then' of undefined"。由于某种原因,承诺没有被退回。我对使用 redux thunk 也特别陌生。
减速机
import { merge } from 'lodash';
import * as APIutil from '../util/articles_api_util';
import {
ArticleConstants
} from '../actions/article_actions';
const ArticlesReducer = (state = {}, action) => {
switch (action.type) {
case ArticleConstants.RECEIVE_ALL_ARTICLES:
debugger
return merge({}, action.articles);
default:
return state;
}
};
export default ArticlesReducer;
商店
import { createStore, applyMiddleware } from 'redux';
import RootReducer from '../reducers/root_reducer';
import thunk from 'redux-thunk';
import * as APIUtil from '../util/articles_api_util';
export const ArticleConstants = {
RECEIVE_ALL_ARTICLES: "RECEIVE_ALL_ARTICLES",
REQUEST_ALL_ARTICLES: "REQUEST_ALL_ARTICLES"
}
操作数
export function fetchArticles() {
return function(dispatch) {
return APIUtil.fetchArticles().then(articles => {
dispatch(receiveAllArticles(articles));
}).catch(error => {
throw(error);
});
};
}
export const requestAllArticles= () => ({
type: REQUEST_ALL_ARTICLES
});
export const receiveAllArticles = articles => ({
type: RECEIVE_ALL_ARTICLES,
articles
});
const configureStore = (preloadedState = {}) => (
createStore(
RootReducer,
preloadedState,
applyMiddleware(thunk)
)
);
export default configureStore;
APIUtil
export const fetchArticles = (success) => {
$.ajax({
method: 'GET',
url: `/api/articles`,
success,
error: ()=> (
console.log("Invalid Article")
)
});
};
箭头函数只做隐含的 return
s 如果你离开大括号。一旦包含花括号,就定义了一个函数体,需要显式 return
一个值。
您的 fetchArticles
函数被写成带有花括号的箭头函数。但是,您并未明确 returning $.ajax()
调用的结果。因此,该函数的 return 值为 undefined
,并且没有承诺 returned 可以链接。