简体   繁体   English

获取TypeError:无法读取未定义错误的属性“map”

[英]Getting TypeError: Cannot read property 'map' of undefined error

I am trying to learn react js with redux. 我正在尝试用redux学习反应js。

This is my View.js 这是我的View.js

export default HomeView

//Import React
import React from 'react'

//Import Redux Components
import { connect } from 'react-redux';

//Import Action
import { addAcct, upsertAccts, qryAccts, updateAccts } from '../../Counter/modules/counter';

class HomeView extends React.Component {
  constructor (props)
  {
    super(props);
  }
  componentWillMount()
  {
    this.props.dispatch(qryAccts());
    console.log(this.props);
    this.forceUpdate();
  }
  componentDidMount()
  {
    console.log('----Did----',this.props.accts);
  }
  //Perform Upsert Actions
  upsertAccts(e)
  {
    this.props.dispatch(upsertAccts(this.props.accts))
  }

  //Add new Account to the Array
  addAcct(event)
  {
    this.props.dispatch(addAcct());
    console.log('===this.props===',this.props);
  }


  //onChange function to handle when a name is changed
  handleChange(ev,index)
  {
    this.props.dispatch(updateAccts(ev.target.value,index));
  }

  dateChange(e)
  {
    console.log(e.target.value);
  }

  render() {
    console.log('-----this.props.accts-------',this.props.accts);
    return (
      <div>
        <h1>Accounts</h1>
        <ul>
         { this.props.accts.map((v,i) => <li key={i}><input style={{width:'500px'}} onChange={event => this.handleChange(event,i)} value={v.Name}/></li>) }
        </ul>
      </div>
    );
  }
}

//Connects Redux State to React, Injects reducer as a property
//export default Page1Demo;
export default connect(state => ({ accts: state.accts }))(HomeView);

And my reducer action here 我的减速机动作在这里

// ------------------------------------
// Constants
// ------------------------------------


export const RECEIVED_ACCTS = 'RECEIVED_ACCTS';
export const UPSERT_ACCTS = 'UPSERT_ACCTS';
export const ADD_ACCT = 'ADD_ACCT';
export const UPDATE_ACCTS = 'UPDATE_ACCTS';


// ------------------------------------
// Actions
// ------------------------------------
export function recAccts(accts) {
  console.log('------16----',accts);
  return{
    type: RECEIVED_ACCTS,
    accts: accts
  }
}

export function qryAccts()
{
  return dispatch => {
    ResponsiveCtrl.getAccts(function(r, e) {
      console.log('------26----',r);
      console.log('------26----',e);
      dispatch(recAccts(r))
    },{escape:false});
  }
}

export function upsertAccts(acctStore) {
  return dispatch => {
    ResponsiveCtrl.upsertAccts(acctStore, function(r, e) {
      dispatch(recAccts(r))
    },{escape:false});
  }
}

export function addAcct() {
  return {
    type: ADD_ACCT
  }
}

export function updateAccts(name,index) {
  return {
    type: UPDATE_ACCTS,
    acctName:name,
    listIndex:index
  }
}

/*  This is a thunk, meaning it is a function that immediately
    returns a function for lazy evaluation. It is incredibly useful for
    creating async actions, especially when combined with redux-thunk!

    NOTE: This is solely for demonstration purposes. In a real application,
    you'd probably want to dispatch an action of COUNTER_DOUBLE and let the
    reducer take care of this logic.  */



// ------------------------------------
// Action Handlers
// ------------------------------------

// ------------------------------------
// Reducer
// ------------------------------------
const initialState = [];

//Reducer function
export function accts(state = initialState, action) {
  console.log('==action===',action);
  console.log('==initialState===',initialState);
  console.log('==state===',state);
  switch (action.type) {
  case RECEIVED_ACCTS:
  //Return the Accouts we receive from Salesforce
    state = action.accts;
    return state;
  case UPDATE_ACCTS:
  //Update our array at the specific index
    var newState = state.slice();
    newState[action.listIndex].Name = action.acctName;
    return newState;
  case ADD_ACCT:
  //Add a new Account to our Array
    var newState = state.slice();
    newState.push({Name:""});
    return newState;
  default:
    return state
  }
}

I am able to determine the issue but not sure how to fix the issue. 我能够确定问题但不确定如何解决问题。

Issue is dom is generated first and I am getting the values later that's why my map is empty initially and it is giving me error 问题是首先生成dom,然后我得到值,这就是为什么我的地图最初是空的并且它给了我错误

TypeError: Cannot read property 'map' of undefined TypeError:无法读取未定义的属性“map”

Any idea how to fix this issue. 知道如何解决这个问题。

在此输入图像描述

Break that code into its own method with a validation on this.props.accts . 通过对this.props.accts的验证, this.props.accts代码分解为自己的方法。

...

renderAccts () {
  if (Array.isArray(this.props.accts)) {
    return this.props.accts.map((v,i) => <li key={i}><input style={{width:'500px'}} onChange={event => this.handleChange(event,i)} value={v.Name}/></li>)
  }
  return ''
},

render () {
...
<div>
  <h1>Accounts</h1>
  <ul>
    {this.renderAccts()}
  </ul>
</div>
}

This is good practice to begin with, because you want to ensure that you are getting exactly what you expect from props , but it will also let you debug further if it turns out that your initial assumption is not the cause of the problem. 这是一个很好的做法,因为你想要确保你得到你对props期望,但如果事实证明你的初始假设不是问题的原因,它也会让你进一步调试。 You can now easily add a console.log(this.props.accts) before the map and see what's going on. 您现在可以在地图前轻松添加console.log(this.props.accts) ,看看发生了什么。 This method will be called every time you receive new props (which triggers render). 每次收到新的道具(触发渲染)时都会调用此方法。 If that prop is undefined the first time render is called, you will get this error if you try to map over it. 如果在第一次调用render时未定义该prop,则在尝试映射时会出现此错误。 If the problem persists, even with this validation, you should ensure you are ever actually getting the prop you expect. 如果问题仍然存在,即使进行了此验证,您也应确保实际获得预期的道具。

暂无
暂无

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

相关问题 ×获取TypeError:无法读取未定义的属性“ map” - × Getting a TypeError: Cannot read property 'map' of undefined 得到这个类型错误:无法读取未定义的属性“地图” - getting this TypeError: Cannot read property 'map' of undefined TypeError:无法读取未定义的属性“地图”错误 - TypeError: Cannot read property 'map' of undefined" error 尝试映射数组时出现错误。 TypeError:无法读取未定义的属性“ map” - Getting error while trying to map over array. TypeError: Cannot read property 'map' of undefined 我试图 map 数据库中的数据并收到此错误。 TypeError:无法读取未定义的属性“地图” - I trying to map the data from the database and getting this error. TypeError: Cannot read property 'map' of undefined 我不断收到此类型错误:无法读取未定义的属性“地图” - I keep getting this TypeError: Cannot read property 'map' of undefined 我在我的 React 项目中收到此错误“TypeError: Cannot read property 'map' of undefined” - I'm getting this error “TypeError: Cannot read property 'map' of undefined” on my React project 为什么我收到错误:类型错误:无法读取未定义的属性“地图”? - Why am I getting an error : TypeError: Cannot read property 'map' of undefined? React.js 错误:TypeError:无法读取未定义的属性“地图” - React.js error :TypeError: Cannot read property 'map' of undefined 错误:未被捕获的TypeError:无法读取未定义的属性“映射” - Error: Uncaught TypeError: Cannot read property 'map' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM