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

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:

So I tried to create a scatterplot between service_evaluation.category1 and queued_duration with a tooltip for other fields:

  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:

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

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!

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!