无法读取未定义的属性'then'

aca*_*222 2 javascript redux redux-thunk react-redux

所以我正在使用react + redux,我继续得到以下错误:"无法读取属性'然后'未定义".由于某种原因,承诺不会被退回.我也特别擅长使用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;
Run Code Online (Sandbox Code Playgroud)

商店

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"
}
Run Code Online (Sandbox Code Playgroud)

操作

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;
Run Code Online (Sandbox Code Playgroud)

APIUtil

export const fetchArticles = (success) => {
  $.ajax({
        method: 'GET',
        url: `/api/articles`,
        success,
        error: ()=> (
        console.log("Invalid Article")
      )
    });
};
Run Code Online (Sandbox Code Playgroud)

mar*_*son 5

return如果你不使用花括号,箭头函数只会执行隐式s.只要包含花括号,就定义了一个函数体,并且需要显式地return赋值.

您的fetchArticles函数被编写为带有花括号的箭头函数.但是,您没有显式返回$.ajax()调用的结果.所以,函数的返回值是undefined,并且没有返回可以链接的承诺.