简体   繁体   English

如何修复:'TypeError:fetchProducts不是函数'

[英]How to fix: 'TypeError: fetchProducts is not a function'

I am trying to retrieve a set of products from an API using a redux store. 我正在尝试使用redux存储从API检索一组产品。

To achieve this, I created actions and reducers, a function mapStateToProps() and a function mapDispatchToProps() , the latter one contains a function fetchProducts() , which is defined in the action file and should retrieve the list of products when called. 为了实现这一点,我创建了动作和mapStateToProps()器,一个函数mapStateToProps()和一个函数mapDispatchToProps() ,后者包含一个函数fetchProducts() ,它在动作文件中定义,并且应该在调用时检索产品列表。

All of this is done in the following way: 所有这些都是通过以下方式完成的:

This is the overview component, which should render the data (the actual rendering is left out, because it does not affect this question): 这是概述组件,它应该呈现数据(实际渲染被省略,因为它不会影响这个问题):

import React, {Component} from 'react';
import {connect} from 'react-redux';
import PropTypes from 'prop-types'
import fetchProductsAction from '../actions/ProductFetchingActionFile'
import {bindActionCreators} from 'redux';

export class Overview extends Component {
    constructor(props) {
        super(props);
    }
    componentDidMount() {
        this.props.fetchProducts();
    }

    render() {
        return(
                  <h1>test</h1>
        )
    }
}

const mapStateToProps = state => ({
    error: state.rootReducer.productsReducer.error,
    products: state.rootReducer.productsReducer.products,
    pending: state.rootReducer.productsReducer.pending,
})

const mapDispatchToProps = dispatch => bindActionCreators({
    fetchProducts: fetchProductsAction 
}, dispatch)

Overview.propTypes = {
    fetchProducts: PropTypes.func
}

export default connect(mapStateToProps, mapDispatchToProps)(Overview);

This is the action file (ProductFetchingActionFile): 这是动作文件(ProductFetchingActionFile):

import {
    PREFIX,
    FETCH_PRODUCTS_PENDING,
    FETCH_PRODUCTS_SUCCESS,
    FETCH_PRODUCTS_ERROR
} from "./types";

function fetchProductsPending() {
    return{
        type: FETCH_PRODUCTS_PENDING
    }
}

function fetchProductsSuccess(products) {
    return{
        type: FETCH_PRODUCTS_SUCCESS,
        products: products
    }
}

function fetchProductsError(error) {
    return{
        type: FETCH_PRODUCTS_ERROR,
        error: error
    }
}

function fetchProducts() {
    return dispatch => {
        dispatch(fetchProductsPending());
        return fetch(`localhost:8080/products`)
            .then(res => res.json())
            .then(res => {
                if(res.error) {
                    throw(res.error);
                }
                dispatch(fetchProductsSuccess(res));
                return res
            })
            .catch(error => {
                dispatch(fetchProductsError(error));
            })
    }
}

export default fetchProducts

This is the reducer file: 这是reducer文件:

import {
    FETCH_PRODUCTS_PENDING,
    FETCH_PRODUCTS_SUCCESS,
    FETCH_PRODUCTS_ERROR
} from "./types"


const initialState = {
    pending: false,
    products: [],
    error: null
}

export function productsReducer(state = initialState, action) {
    switch(action.type) {
    case FETCH_PRODUCTS_PENDING:
        return {
            ...state,
            pending: true
        }
    case FETCH_PRODUCTS_SUCCESS:
        return {
            ...state,
            pending: false,
            products: action.products
        }
    case FETCH_PRODUCTS_ERROR:
        return {
            ...state,
            pending: false,
            error: action.error
        }
    default:
        return state;
    }
}

Now, I get the following error when the Overview component is loaded: TypeError: fetchProducts is not a function . 现在,加载Overview组件时出现以下错误: TypeError: fetchProducts is not a function This error fires on the call to fetchProducts in the componentDidMount() function. componentDidMount()函数中调用fetchProducts会触发此错误。

Also, when the Overview.props is printed in the console before this call, it does not contain the function fetchProducts , so I suspect that it has to do something with the mapDispatchToProps , but I cannot find the problem. 此外,当在调用之前在控制台中打印Overview.props时,它不包含函数fetchProducts ,因此我怀疑它必须对mapDispatchToProps执行某些mapDispatchToProps ,但我找不到问题。

Edit: it is good to note that if the imported fetchProductsAction is printed in the constructor of the Overview component, it shows the correct function. 编辑:最好注意,如果在Overview组件的构造函数中打印导入的fetchProductsAction ,它将显示正确的函数。 It does however not end up in the props of Overview , so I expect the problem to be there. 然而它确实不会出现在Overviewprops中,所以我希望问题存在。

try this 尝试这个

   const mapDispatchToProps = dispatch => {
       fetchProducts : () => {
            dispatch(fethcProducts())
         }
   }

You're importing your action as 您正在将您的操作导入为

import fetchProductsAction from '../actions/ProductFetchingActionFile'

when it looks like it's not being exported as a default. 当它看起来没有被导出为默认值时。

It should probably be: 应该是:

import { fetchProductsAction } from '../actions/ProductFetchingActionFile'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复TypeError:registerUser不是函数 - How to fix TypeError: registerUser is not a function 如何修复 Uncaught TypeError: mapster is not a function? - How to fix Uncaught TypeError: mapster is not a function? 如何修复&#39;TypeError:(images || [])。forEach不是一个函数&#39; - How to fix 'TypeError: (images || []).forEach is not a function' 如何修复 TypeError:navigation.setOptions 不是 function - How to fix TypeError: navigation.setOptions is not a function 如何修复“TypeError:System.config 不是函数” - How to fix ''TypeError: System.config is not a function' 如何修复 TypeError 不是函数(用 Jest 测试 promise) - How to fix TypeError is not a function (testing promises with Jest) 如何修复 ReactTable 的“TypeError: resolveData(...).map is not a function” - How to fix 'TypeError: resolveData(...).map is not a function' for ReactTable 如何修复 React 中的“TypeError:moment is not a function”? - How to fix 'TypeError: moment is not a function' in React? Gulpfile.js:如何修复“ TypeError:“ listener”参数必须是一个函数”? - Gulpfile.js : How to fix “TypeError: ”listener“ argument must be a function”? JavaScript TypeError:回调不是函数-我该如何解决? - Javascript TypeError: callback is not a function - how do i fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM