鍍金池/ 問答/HTML/ 這個為什么會報auditRuleList.result.map is not a

這個為什么會報auditRuleList.result.map is not a function這個錯呢?

這個為什么會報TypeError: auditRuleList.result.map is not a function這個錯呢?

import { Modal,Steps} from 'antd';
import React from 'react';
import {connect} from "react-redux";

const Step = Steps.Step;
class ApproveState extends React.Component{

constructor(props) {
    super(props);
    this.state={
        visible: false,
        auditor:false,

    };
}

componentWillMount() {

}

componentDidMount() {

}

render(){
    const {visible,onCancel,currentAudit,auditRuleList} = this.props;
    console.log(auditRuleList)
    const dataSource = auditRuleList ? auditRuleList.result ? auditRuleList.result.map((item,index)=><Step
        key={item.id} title={item.name} description={item.time} />) :[]:[];
   
    return (
        <Modal
            visible={visible}
            title={"審核情況"}
            width="80vw"
            onCancel={onCancel}
            onOk={onCancel}
        >

        <Steps progressDot current={5}>
                     {dataSource}
        </Steps>
        </Modal>
    );
}
}

function mapStateToProps(state) {

return {
    auditRuleList:state.approveReducer.auditRuleList,
}
}

function mapDispatchToProps(dispatch){

return{

}
}
export default connect(mapStateToProps,mapDispatchToProps)(ApproveState);
回答
編輯回答
遲月

因為auditRuleList.result不是數組,原型鏈上不存在map方法

2018年7月24日 21:44
編輯回答
涼汐
(1)TypeError類型的錯誤: 對一個變量的值做不合理的操作 如對非函數的變量進行函數調用 或者引用null或undefined類型的值得屬性等等
(2)map()是數組(Array)對象的方法: array.map(function(currentValue,index,arr))
 (3)改法:
const dataSource = auditRuleList ? (auditRuleList.result && Array.isArray(auditRuleList.result))? auditRuleList.result.map((item,index)=><Step
        key={item.id} title={item.name} description={item.time} />) :[]:[];
2018年7月29日 22:34