简体   繁体   English

React / Webpack / Django-未捕获的TypeError:无法读取未定义的属性“ XXX”

[英]React/Webpack/Django - Uncaught TypeError: Cannot read property 'XXX' of undefined

I am trying to create a React component called 'Proposals' that will render a tabular list of information received from the Django backend. 我正在尝试创建一个称为'Proposals'的React组件,该组件将呈现从Django后端接收的信息的表格列表。

I am using the Reactable-Search component to form the table, but I've kept getting an error when I try to map the this.props.proposals values such as id, and proj_name to the table cells - Uncaught TypeError: Cannot read property 'cells' of undefined 我正在使用Reactable-Search组件来构成表格,但是当我尝试将this.props.proposals值(例如id和proj_name)映射到表格单元格时,我一直收到错误消息-Uncaught TypeError:Cannot read property未定义的“单元”

Really not sure why because when I map this.props.proposals directly in the render return of a typical html table tags it is working ie rendering the backend data ok. 真的不确定为什么,因为当我直接在典型的html表标签的渲染返回中映射this.props.proposals时,它就可以正常工作,即可以正常渲染后端数据。 and I've also used the Reactable-Search component with the mapping in other cases and it's worked fine. 在其他情况下,我也将Reactable-Search组件与映射一起使用,效果很好。

Log output of this.props.proposals shows the correct array of objects also...: this.props.proposals的日志输出还显示正确的对象数组...: 屏幕截图

Really appreciate if someone can nudge me in the right direction, thanks! 非常感谢有人可以向正确的方向推我,谢谢!

The Proposals component: 投标组件:

 import React, { Component } from "react"; import { connect } from "react-redux"; import SearchTable from "reactable-search"; import { proposals } from "../actions"; class Proposals extends Component { componentDidMount() { this.props.fetchProposals(); } constructor(props) { super(props); this.state = {}; } render() { var rows = this.props.proposals.map(p => ({ selected: this.state.selectedRow === p.id, onClick: () => { this.setState({ selectedRow: p.id }); }, cells: { "#": p.id, "Project Name": p.proj_name } })); return ( <SearchTable showExportCSVBtn={true} searchPrompt="Type to search" rows={rows} /> ); } } const mapStateToProps = state => { return { proposals: state.proposals }; }; const mapDispatchToProps = dispatch => { return { fetchProposals: () => { dispatch(proposals.fetchProposals()); } }; }; export default connect( mapStateToProps, mapDispatchToProps )(Proposals); 

The proposals reducer: 建议减速器:

 const initialState = []; export default function proposals(state = initialState, action) { switch (action.type) { case "FETCH_PROPOSALS": return [...action.proposals]; default: return state; } } 

The proposals action 提案行动

 export const fetchProposals = () => { return dispatch => { let headers = { "Content-Type": "application/json" }; return fetch("/api/proposals/", { headers }) .then(res => res.json()) .then(proposals => { return dispatch({ type: "FETCH_PROPOSALS", proposals }); }); }; }; 

The problem is that you are requesting the proposals asynchronously but the SearchTable component doesn't seem to work with empty initial proposals object. 问题是您正在异步请求投标,但是SearchTable组件似乎无法与空的初始投标对象一起使用。 Try passing in an empty array as its rows prop and you'll get the exact same error message about undefined object. 尝试传递一个空数组作为其rows属性,您将得到与未定义对象完全相同的错误消息。

To fix this you need to show a loading indicator instead of the SearchTable while the proposals are being fetched. 要解决此问题,您需要在获取建议时显示一个加载指示器,而不是SearchTable Your reducer should look something like this, except you should also handle failure case: 减速器应该看起来像这样,除了您还应该处理故障情况:

const initialState = { isLoading: false, error: null, proposals: [] };

export default function proposals(state = initialState, action) {
  switch (action.type) {
    case "FETCH_PROPOSALS":
      return {
        ...state,
        isLoading: true
      };
    case "FETCH_PROPOSALS_SUCCESS":
      return {
        ...state,
        isLoading: false,
        proposals: action.proposals
      };
    case "FETCH_PROPOSALS_FAILURE":
      return {
        ...state,
        isLoading: false,
        error: action.error,
      };
    default:
      return state;
  }
}

The component should then render an activity indicator or loading status or anything other than SearchTable when isLoading is active: 然后,在isLoading处于活动状态时,组件应呈现活动指示符或加载状态或除SearchTable之外的任何内容:

import React, { Component } from "react";
import { connect } from "react-redux";
import SearchTable from "reactable-search";
import { proposals } from "../actions";

class Proposals extends Component {
  componentDidMount() {
    this.props.fetchProposals();
  }
  constructor(props) {
    super(props);
    this.state = {};
  }

  render() {
    const { proposals, error, isLoading } = this.props;

    if (isLoading) {
      return <div>Loading...</div>;
    }
    if (error) {
      return <div>{error.message}</div>;
    }
    if (proposals.length === 0) {
      return <div>No proposals</div>;
    }

    var rows = proposals.map(p => ({
      selected: this.state.selectedRow === p.id,
      onClick: () => {
        this.setState({ selectedRow: p.id });
      },
      cells: {
        "#": p.id,
        "Project Name": p.proj_name
      }
    }));

    return (
      <SearchTable
        showExportCSVBtn={true}
        searchPrompt="Type to search"
        rows={rows}
      />
    );
  }
}

const mapStateToProps = state => {
  return {
    proposals: state.proposals.proposals,
    isLoading: state.proposals.isLoading,
    error: state.proposals.error,
  };
};

const mapDispatchToProps = dispatch => {
  return {
    fetchProposals: () => {
      dispatch(proposals.fetchProposals());
    }
  };
};

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

And the thunk action: 和重击动作:

export const fetchProposals = () => {
  return dispatch => {
    dispatch({type: "FETCH_PROPOSALS"});
    let headers = { "Content-Type": "application/json" };
    return fetch("/api/proposals/", { headers })
      .then(res => res.json())
      .then(proposals => {
        dispatch({
          type: "FETCH_PROPOSALS_SUCCESS",
          proposals
        });
      })
      .catch(error => {
        dispatch({
          type: "FETCH_PROPOSALS_FAILURE",
          error,
        });
      });
  };
};

暂无
暂无

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

相关问题 反应-未捕获的TypeError:无法读取未定义的属性“ 1” - React - Uncaught TypeError: Cannot read property '1' of undefined Webpack Uncaught TypeError:无法读取未定义的属性“调用” - Webpack Uncaught TypeError: Cannot read property 'call' of undefined 未捕获的类型错误:无法读取 __webpack_require__ 处未定义的属性“调用” - Uncaught TypeError: Cannot read property 'call' of undefined at __webpack_require__ TypeError:无法读取未定义jQuery的属性“ xxx” - TypeError: Cannot read property 'xxx' of undefined jQuery Django jquery数据表:未捕获的TypeError:无法读取未定义的属性&#39;length&#39; - Django jquery datatable: Uncaught TypeError: Cannot read property 'length' of undefined React - Uncaught TypeError:无法读取未定义的属性&#39;func&#39; - React - Uncaught TypeError: Cannot read property 'func' of undefined React Uncaught TypeError:无法读取未定义的属性“ __reactAutoBindMap” - React Uncaught TypeError: Cannot read property '__reactAutoBindMap' of undefined 反应:未捕获的类型错误:无法读取未定义的属性“i” - React : Uncaught TypeError: Cannot read property 'i' of undefined React JS-未捕获的TypeError:无法读取未定义的属性“ bind” - React JS - Uncaught TypeError: Cannot read property 'bind' of undefined 未捕获的TypeError:无法在React中读取未定义的属性&#39;showBarChart&#39; - Uncaught TypeError: Cannot read property 'showBarChart' of undefined in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM