# Getting - entities is possibly 'undefined' error

**URL:** <https://community.dynatrace.com/t/getting-entities-is-possibly-undefined-error/38618>\
**Category:** Developer Q&A Forum\
**Tags:** coding-assistance\
**Created:** [February 27, 2023, 6:11pm UTC](https://community.dynatrace.com/t/getting-entities-is-possibly-undefined-error/38618 "2023-02-27T18:11:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SantiPalacios](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/santipalacios/32/21450_2.png) [@SantiPalacios](https://community.dynatrace.com/u/SantiPalacios)\
**Post date:** [February 27, 2023, 6:11pm UTC](https://community.dynatrace.com/t/getting-entities-is-possibly-undefined-error/38618/1 "2023-02-27T18:11:00Z")

</div>

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.

```c-mrkdwn__pre
const [entitiesArray, setEntitiesArray] = useState<any[]>([]);
  useEffect(() => {
    monitoredEntitiesClient.getEntities(
      {
        entitySelector: 'type("HOST"),tag("OpenShift_App_Nodes")',
        from: 'now-1y/M',
        pageSize: 900
      })
      .then(response => {
        // console.log('response:', response)
        const entities = response?.entities
        console.log('entities:', entities)
        setEntitiesArray(entities.map(entity => entity?.entityId))
      })
  }, []);

```

---

<div class="post-metadata">

**Author:** ![SantiPalacios](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/santipalacios/32/21450_2.png) [@SantiPalacios](https://community.dynatrace.com/u/SantiPalacios)\
**Post date:** [February 27, 2023, 6:37pm UTC](https://community.dynatrace.com/t/getting-entities-is-possibly-undefined-error/38618/2 "2023-02-27T18:37:32Z")

</div>

From&nbsp;[@Michael\_Beemer](https://community.dynatrace.com/user/31655)&nbsp;:

Basically, you’re using&nbsp;[optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)&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&nbsp;[falsy value](https://developer.mozilla.org/en-US/docs/Glossary/Falsy). That will set entities to an empty array.&nbsp;The&nbsp;`??`&nbsp; is called&nbsp;[nullish coalescing](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing)&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.

Hope that’s useful and makes sense.

---

<div class="post-metadata">

**Author:** ![Michael\_Beemer](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/michael_beemer/32/41785_2.png) [@Michael\_Beemer](https://community.dynatrace.com/u/Michael_Beemer)\
**Post date:** [February 27, 2023, 7:19pm UTC](https://community.dynatrace.com/t/getting-entities-is-possibly-undefined-error/38618/3 "2023-02-27T19:19:19Z")

</div>

I recommended the following changes:

Original code:

```javascript
const entities = response?.entities;

```

Proposed change:

```javascript
const entities = response?.entities || [];

```
