【问题标题】:How to make a check for compliance?如何进行合规检查?
【发布时间】:2018-10-02 06:49:59
【问题描述】:

当我输入搜索时出现错误Cannot read property 'general' of undefined 数组中没有现有数据。我需要什么条件来跟踪错误?

当我尝试设置条件时,我失败了

{
   (selectUser.general === !undefined) ?

    <Grid >
       <Grid.Column mobile={16} tablet={6} computer={5}>
          <Image src={selectUser.general.avatar} />
       </Grid.Column>
    <Grid.Column mobile={16} tablet={10} computer={11}>
      <h1>{`${selectUser.general.firstName} ${selectUser.general.lastName}`}</h1>
      <h3>Position: {selectUser.job.title}</h3>
      <h3>Company: {selectUser.job.company}</h3>

      <h4>
         Contact:
         <p><Icon name='mail' size='small' /> <a href="mailto:Gerry_Hackett77@gmail.com">{selectUser.contact.email}</a></p>
         <p><Icon name='phone'  size='small' /> <a href="tel:8959840132">{selectUser.contact.phone}</a></p>
      </h4>
      <p><strong>Address:</strong> {`${selectUser.address.street}, ${selectUser.address.city}, ${selectUser.address.country}`}</p>
     </Grid.Column>
   </Grid>

   :

   <h2>Error</h2>

 }

完整代码示例here

【问题讨论】:

    标签: javascript reactjs redux


    【解决方案1】:

    错误表明 selectUser 未定义,因此您需要在使用它之前检查它是否存在

    {
       (selectUser && selectUser.general) ?
    
        <Grid >
           <Grid.Column mobile={16} tablet={6} computer={5}>
              <Image src={selectUser.general.avatar} />
           </Grid.Column>
        <Grid.Column mobile={16} tablet={10} computer={11}>
          <h1>{`${selectUser.general.firstName} ${selectUser.general.lastName}`}</h1>
          <h3>Position: {selectUser.job.title}</h3>
          <h3>Company: {selectUser.job.company}</h3>
    
          <h4>
             Contact:
             <p><Icon name='mail' size='small' /> <a href="mailto:Gerry_Hackett77@gmail.com">{selectUser.contact.email}</a></p>
             <p><Icon name='phone'  size='small' /> <a href="tel:8959840132">{selectUser.contact.phone}</a></p>
          </h4>
          <p><strong>Address:</strong> {`${selectUser.address.street}, ${selectUser.address.city}, ${selectUser.address.country}`}</p>
         </Grid.Column>
       </Grid>
    
       :
    
       <h2>Error</h2>
    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 2021-12-01
      • 2018-07-02
      相关资源
      最近更新 更多