# Query result brings expected data but I get the error "Cannot read property '...' of undefined"

**URL:** <https://discuss.elastic.co/t/query-result-brings-expected-data-but-i-get-the-error-cannot-read-property-of-undefined/241027>\
**Category:** Kibana\
**Tags:** vega\
**Created:** [July 13, 2020, 6:45pm UTC](https://discuss.elastic.co/t/query-result-brings-expected-data-but-i-get-the-error-cannot-read-property-of-undefined/241027 "2020-07-13T18:45:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SClovesgtx](https://avatars.discourse-cdn.com/v4/letter/s/71e660/32.png) [@SClovesgtx](https://discuss.elastic.co/u/SClovesgtx)\
**Post date:** [July 13, 2020, 6:46pm UTC](https://discuss.elastic.co/t/query-result-brings-expected-data-but-i-get-the-error-cannot-read-property-of-undefined/241027/1 "2020-07-13T18:46:00Z")

</div>

I started to encounter the "Cannot read property '...' of undefined" error for the following document fields:

- **operators** : is an array of objects and I expected to access this value when applying the transformation using the following path at the time {calculate: "datum.\_source['operators'][0]['name']", as: "Operador"}
- **service\_evaluation** : is an object that has a field of interest called "category1".

Some documents do not have these fields and I hoped that vega-lite would handle the missing data just as it managed to handle it in another view it had created.

As I started to encounter this "Cannot read property ..." error I decided to create a query that would guarantee the existence of these fields.

I got the result expected by the dev tolls:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/8/58a6aba194d2fc7ed0ac3ba0468df37acee75ca0.png)

So I tried to create a scatterplot between service\_evaluation.category1 and queued\_duration with a tooltip for other fields:

```auto
  name: "conversation",
  $schema: https://vega.github.io/schema/vega-lite/v2.json
 "title": {"text":"Espera Na Fila x Tempo com o Operador",
           "fontSize": 30
 },
 "selection": {
    "paintbrush": {
      "type": "multi",
      "on": "mouseover", "empty": "none"
    }
  },
  data: {
    # URL object is a context-aware query to Elasticsearch
    url: {
      # The %-enclosed keys are handled by Kibana to modify the query
      # before it gets sent to Elasticsearch. Context is the search
      # filter as shown above the dashboard. Timefield uses the value 
      # of the time picker from the upper right corner.
      // %context%: true
      // %timefield%: queued_datetime
      index: prd-inss-omniv2_conversation
      body: {
          "size": 100, 
          "_source": {
            "includes": ["queued_datetime", 
                      "queued_duration", 
                      "operator_duration",
                      "operators",
                      "infos_user",
                      "service_evaluation",
                      "groups"]
          },
          "query": {
            "bool": {
              "must": [
                {
                  "exists": {
                    "field": "queued_datetime"
                  }
                },
                {
                  "exists": {
                    "field": "queued_duration"
                  }
                },
                {
                  "exists": {
                    "field": "service_evaluation.category1"
                  }
                },
                {
                  "exists": {
                    "field": "operator_duration"
                  }
                },
                {
                  "exists": {
                    "field": "operators.name"
                  }
                }
              ]
            }
        }

      }
        # We only need the content of hits.hits array
        format: {property: "hits.hits"}
    }
  }
  # Parse timestamp into a javascript date value
  transform: [
    {calculate: "toDate(datum._source['queued_datetime'])", 
     as: "tempo"},
    {calculate: "datum._source['operator_duration']", 
    as: "tempo_com_operador"},
    {calculate: "datum._source['queued_duration']", 
    as: "espera_na_fila"},
    {calculate: "datum._source['infos_user']['user_name']", 
    as: "Usuário"},
    {calculate: "datum._source['operators'][0]['name']", 
    as: "Operador"},
    {calculate: "datum._source['groups'][0]['name']", 
    as: "Grupo"}
    {calculate: "datum._source['service_evaluation']['category1']", 
    as: "avaliacao"}
  ]
  # Draw a circle, with x being the time field, and y - number of bytes
 "mark": "point",
  encoding: {
    x: {field: "espera_na_fila", 
        type: "quantitative",
        axis: {title: "Tempo de Espera da Fila (min)"}
    }
    y: {field: "avaliacao", type: "quantitative",
       axis: {title: "Avaliação do Serviço (1 a 5 estrelas)"}
    }
   "tooltip": [
      {"field": "espera_na_fila", "type": "quantitative"},
      {field: "tempo_com_operador", type: "quantitative"},
      {field: "Usuário", type: "nominal"},
      {field: "Operador", type: "nominal"},
      {field: "Grupo", type: "nominal"},
      {field: "avaliacao", type: "nominal"},
    ]
    color: {field:"Grupo", type:"nominal",
      fill: "#f5425a",
      legend: {title:"Grupo"}
      },
    "size": {
      "condition": {
        "selection": "paintbrush", "value": 500
      },
      "value": 50
    }
  }
}

```

But I get the errors:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/e/ce594e5cba5477971303ff6b47596a9db6801d6d.png)

I used debug to see if the expected values of the query:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/1/21f88fed09d8fd8bcaa15a1717481540aad7d5e1.png)

I don't know what to do to get rid of such errors and finally access the data that is being returned by the query. Help please!

---

<div class="post-metadata">

**Author:** ![Midas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/midas/32/50275_2.png) [@Midas](https://discuss.elastic.co/u/Midas)\
**Post date:** [July 20, 2020, 12:38am UTC](https://discuss.elastic.co/t/query-result-brings-expected-data-but-i-get-the-error-cannot-read-property-of-undefined/241027/2 "2020-07-20T00:38:47Z")

</div>

Cloves,

I don't deal too much in Vega-Lite but calculate transforms work exactly the same as Vega formula transforms. In this regard, they are expressions. You should be able to write a conditional to handle this from now on. Like "datum.\_source['operator\_duration'] ? datum.\_source['operator\_duration'] : 0" or "if(datum.\_source['operator\_duration'], datum.\_source['operator\_duration'], 0)". Basically you have to add in a conditional or else Vega will fail when it cannot find what you are declaring.

Please let me know if that helps!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 17, 2020, 12:38am UTC](https://discuss.elastic.co/t/query-result-brings-expected-data-but-i-get-the-error-cannot-read-property-of-undefined/241027/3 "2020-08-17T00:38:50Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
