简体   繁体   English

使用缺少键的对象映射反应数组导致TypeError崩溃

[英]React array of objects mapping with missing key causing TypeError crash

Upon mapping my searchResults array that gets populated with objects (being returned via elastic search) of my Drug object into my React component, I ran into an issue when testing for a case of an erroneous result with a missing key in my object. 在将用我的Drug对象的对象填充(通过弹性搜索返回)的searchResults数组映射到我的React组件时,在测试对象中缺少键的错误结果的情况下,我遇到了一个问题。 The app understandably crashes with the "TypeError: Cannot read property 'icdCodeNum' of undefined" which in my case is due to the missing "primaryIcdCode" key in my Drug object. 可以理解,该应用程序崩溃并出现"TypeError: Cannot read property 'icdCodeNum' of undefined" ,在我的情况下,这是由于我的Drug对象中缺少“ primaryIcdCode”键所致。 I am going through various conditional blocks to check for the missing data before mapping, but all my solutions end up blocking even correct search results from being returned. 我正在经历各种条件块,以在映射之前检查丢失的数据,但是我所有的解决方案最终都阻止了甚至返回正确的搜索结果。

A normal searchResult array response looks as follows (where all the necessary object keys exist): 正常的searchResult数组响应如下所示(其中所有必需的对象键都存在):

[{"otherIcdCodes":[{"otherIcdCodes":[],"_id":"5bc8393936ca7a5008025a78","brandName":"Advil","genericName":"Ibupr
ofen","primaryIcdCode":{"_id":"5bc838e036ca7a5008025a75","icdCodeNum":"R52","icdCodeValue":"Pain, unspecified"},"dru
gNotes":"Don't take on empty stomach","drugClass":"Pain management","drugSchedule":"0","extraDrugInfo":"","date":"20
18-10-18T07:41:45.183Z","__v":0}]

The problem arises, when my search result object is missing the "primaryIcdCode" key from one of my search responses (due to incorrectly entered data in the database itself, which is the way I discovered the issue). 当我的搜索结果对象缺少我的一个搜索响应中的“ primaryIcdCode”键时,就会出现问题(由于数据库本身输入的数据不正确,这是我发现问题的方式)。

I am mapping my array as follows in my DrugSearch component: 我在我的DrugSearch组件中映射数组如下:

<div>
  <ListGroup>
      {searchResults.map(
        ({
          _id,
          brandName,
          genericName,
          drugNotes,
          primaryIcdCode,
          otherIcdCodes
        }) => (
          <Card key={_id} body>
            <h4>
              <Button disabled className="drug-search-name">
                {brandName} | {genericName}
              </Button>
            </h4>
            <div>
                <Button className="btn-lg-icd">
                  {primaryIcdCode.icdCodeNum}
                </Button>      
          </Card>
        )
      )}
  </ListGroup>
</div>

What would be the proper approach to catch the missing data before the mapping? 在映射之前捕获丢失的数据的正确方法是什么? I tried filtering, but I couldn't configure it properly with my DrugSearch component's logic, so my search results wouldn't even get returned. 我尝试过滤,但无法使用DrugSearch组件的逻辑正确配置它,因此搜索结果甚至都不会返回。

Here is a link to the full component (check DrugSearch.js - ignore the index.js) to make more sense out of the issue: 这是完整组件的链接(请检查DrugSearch.js-忽略index.js),以使问题更合理:

https://codesandbox.io/s/m3p1k31p0j https://codesandbox.io/s/m3p1k31p0j

Thanks in advance! 提前致谢!

If primaryIcdCode code name is an optional property, you must make sure that you are not accessing any internal property within this object without a conditional check 如果primaryIcdCode代码名称是可选属性,则必须确保在没有条件检查的情况下不要访问此对象内的任何内部属性。

<div>
  <ListGroup>
      {searchResults.map(
        ({
          _id,
          brandName,
          genericName,
          drugNotes,
          primaryIcdCode,
          otherIcdCodes
        }) => (
          <Card key={_id} body>
            <h4>
              <Button disabled className="drug-search-name">
                {brandName} | {genericName}
              </Button>
            </h4>
            <div>
               {primaryIcdCode && <Button className="btn-lg-icd">
                  { primaryIcdCode.icdCodeNum}
                </Button>  }    
          </Card>
        )
      )}
  </ListGroup>
</div>

If you don't want to display searchResults without a primaryIcdCode property, you can filter out these results. 如果不想显示没有primaryIcdCode属性的searchResults,则可以过滤掉这些结果。

<div>
  <ListGroup>
      {searchResults.filter((item) => !!item.primaryIcdCode).map(
        ({
          _id,
          brandName,
          genericName,
          drugNotes,
          primaryIcdCode,
          otherIcdCodes
        }) => (
          <Card key={_id} body>
            <h4>
              <Button disabled className="drug-search-name">
                {brandName} | {genericName}
              </Button>
            </h4>
            <div>
                <Button className="btn-lg-icd">
                  {primaryIcdCode.icdCodeNum}
                </Button>
            </div>
          </Card>
        )
      )}
  </ListGroup>
</div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM