<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Getting - entities is possibly 'undefined' error in Developer Q&amp;A Forum</title>
    <link>https://community.dynatrace.com/t5/Developer-Q-A-Forum/Getting-entities-is-possibly-undefined-error/m-p/205759#M205</link>
    <description>&lt;P&gt;&lt;SPAN&gt;I am trying to work on a fairly basic app that calls Dynatrace API for entities and then want to parse out the entityId and build an array of entities. I think that this might be specific to Typescript and not sure how to solve it. I know that I am getting an result array of objects, but when trying to use map, is telling me that entities is possibly undefined. Any help would be appreciated.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="c-mrkdwn__pre" data-stringify-type="pre"&gt;const [entitiesArray, setEntitiesArray] = useState&amp;lt;any[]&amp;gt;([]);
  useEffect(() =&amp;gt; {
    monitoredEntitiesClient.getEntities(
      {
        entitySelector: 'type("HOST"),tag("OpenShift_App_Nodes")',
        from: 'now-1y/M',
        pageSize: 900
      })
      .then(response =&amp;gt; {
        // console.log('response:', response)
        const entities = response?.entities
        console.log('entities:', entities)
        setEntitiesArray(entities.map(entity =&amp;gt; entity?.entityId))
      })
  }, []);&lt;/PRE&gt;</description>
    <pubDate>Thu, 20 Apr 2023 07:49:38 GMT</pubDate>
    <dc:creator>SantiPalacios</dc:creator>
    <dc:date>2023-04-20T07:49:38Z</dc:date>
    <item>
      <title>Getting - entities is possibly 'undefined' error</title>
      <link>https://community.dynatrace.com/t5/Developer-Q-A-Forum/Getting-entities-is-possibly-undefined-error/m-p/205759#M205</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I am trying to work on a fairly basic app that calls Dynatrace API for entities and then want to parse out the entityId and build an array of entities. I think that this might be specific to Typescript and not sure how to solve it. I know that I am getting an result array of objects, but when trying to use map, is telling me that entities is possibly undefined. Any help would be appreciated.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="c-mrkdwn__pre" data-stringify-type="pre"&gt;const [entitiesArray, setEntitiesArray] = useState&amp;lt;any[]&amp;gt;([]);
  useEffect(() =&amp;gt; {
    monitoredEntitiesClient.getEntities(
      {
        entitySelector: 'type("HOST"),tag("OpenShift_App_Nodes")',
        from: 'now-1y/M',
        pageSize: 900
      })
      .then(response =&amp;gt; {
        // console.log('response:', response)
        const entities = response?.entities
        console.log('entities:', entities)
        setEntitiesArray(entities.map(entity =&amp;gt; entity?.entityId))
      })
  }, []);&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Apr 2023 07:49:38 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Developer-Q-A-Forum/Getting-entities-is-possibly-undefined-error/m-p/205759#M205</guid>
      <dc:creator>SantiPalacios</dc:creator>
      <dc:date>2023-04-20T07:49:38Z</dc:date>
    </item>
    <item>
      <title>Re: Getting - entities is possibly 'undefined' error</title>
      <link>https://community.dynatrace.com/t5/Developer-Q-A-Forum/Getting-entities-is-possibly-undefined-error/m-p/205761#M206</link>
      <description>&lt;P&gt;From&amp;nbsp;&lt;a href="https://community.dynatrace.com/t5/user/viewprofilepage/user-id/31655"&gt;@Michael_Beemer&lt;/a&gt;&amp;nbsp;:&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Basically, you're using&amp;nbsp;&lt;/SPAN&gt;&lt;A class="c-link" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining" target="_blank" rel="noopener noreferrer" data-stringify-link="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining" data-sk="tooltip_parent"&gt;optional chaining&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;which returns either the value or undefined. In the solution I proposed ( const entities = response?.entities || []; ), it uses OR logic because if the entities property is missing, undefined is evaluations to a&amp;nbsp;&lt;/SPAN&gt;&lt;A class="c-link" href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy" target="_blank" rel="noopener noreferrer" data-stringify-link="https://developer.mozilla.org/en-US/docs/Glossary/Falsy" data-sk="tooltip_parent"&gt;falsy value&lt;/A&gt;&lt;SPAN&gt;. That will set entities to an empty array.&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;The&amp;nbsp;&lt;/SPAN&gt;&lt;CODE class="c-mrkdwn__code" data-stringify-type="code"&gt;??&lt;/CODE&gt;&lt;SPAN&gt;&amp;nbsp; is called&amp;nbsp;&lt;/SPAN&gt;&lt;A class="c-link" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing" target="_blank" rel="noopener noreferrer" data-stringify-link="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing" data-sk="tooltip_parent"&gt;nullish coalescing&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;( entities?.map(…) ?? [] ) and is useful when the first value should be used if it's any other than undefined, even if it's a falsy value such as 0 or false.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Hope that's useful and makes sense.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Feb 2023 18:39:07 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Developer-Q-A-Forum/Getting-entities-is-possibly-undefined-error/m-p/205761#M206</guid>
      <dc:creator>SantiPalacios</dc:creator>
      <dc:date>2023-02-27T18:39:07Z</dc:date>
    </item>
    <item>
      <title>Re: Getting - entities is possibly 'undefined' error</title>
      <link>https://community.dynatrace.com/t5/Developer-Q-A-Forum/Getting-entities-is-possibly-undefined-error/m-p/205765#M207</link>
      <description>&lt;P&gt;I recommended the following changes:&lt;/P&gt;
&lt;P&gt;Original code:&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;const entities = response?.entities;&lt;/LI-CODE&gt;
&lt;P&gt;Proposed change:&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;const entities = response?.entities || [];&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Feb 2023 19:19:19 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/Developer-Q-A-Forum/Getting-entities-is-possibly-undefined-error/m-p/205765#M207</guid>
      <dc:creator>Michael_Beemer</dc:creator>
      <dc:date>2023-02-27T19:19:19Z</dc:date>
    </item>
  </channel>
</rss>

