gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:46:04 26 4
gpt4 key购买 nike

所以我正在使用 react + redux,并且继续收到以下错误:“无法读取未定义的属性‘then’”。由于某种原因, promise 没有被退回。我对使用 redux thunk 也特别陌生。

reducer

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"
}

Action

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。只要包含大括号,就定义了一个函数体,需要显式返回一个值。

您的 fetchArticles 函数被编写为带有花括号的箭头函数。但是,您并未明确返回 $.ajax() 调用的结果。因此,该函数的返回值为 undefined,并且没有返回您可以链接的 promise 。

关于javascript - 无法读取未定义的属性 'then',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41358159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com