Cannot read properties of undefined (reading 'value') and Cannot read properties of undefined (reading ' metadata')

Cannot read properties of undefined (reading 'value') and Cannot read properties of undefined (reading ' metadata')

My current payload is this:

  1. [ { "variable": "segundo", "value": 9, "minuto": 45, "hora": 13, "dia": 4, "mes": 12, "ano": 23, "valor": 6486, "metadata": { "mqtt_topic": "mensagem/json" } } ]
When I use emulator to send it so I can get the variables of a date and a application value on my bucket (because the same payload was being sent from my device but it was taking too much time to test), it returns the error in the title.

Solutions I tried:

- Emulating with the format it should have AFTER passing my personal parser below:

  1. const ignore_vars = ['minuto','hora','dia','mes','ano','valor'];

    const minuto = payload.find((x) => x.variable === 'minuto');
    const hora = payload.find((x) => x.variable === 'hora');
    const dia = payload.find((x) => x.variable === 'dia');
    const mes = payload.find((x) => x.variable === 'mes');
    const ano = payload.find((x) => x.variable === 'ano');
    const valor = payload.find((x) => x.variable === 'valor');

    const segundoIndex = payload.findIndex((x) => x.variable === 'segundo');

    if (segundoIndex !== -1 && !payload[segundoIndex].hasOwnProperty('metadata')) {
      payload[segundoIndex].metadata = {}
    }

    payload[segundoIndex].metadata.minuto = minuto.value;
    payload[segundoIndex].metadata.hora = hora.value;
    payload[segundoIndex].metadata.dia = dia.value;
    payload[segundoIndex].metadata.mes = mes.value;
    payload[segundoIndex].metadata.ano = ano.value;
    payload[segundoIndex].metadata.valor = valor.value;


    // Remove unwanted variables.
    payload = payload.filter(x => !ignore_vars.includes(x.variable));

(the parser is active)

The parser, in theory, should change the payload to something like:

(I mean, that's what I understood, this method didn't work for me yet)
  1. [
        {
            "variable": "segundo",
            "value": 10,
            "metadata": {
                "mqtt_topic": "mensagem/json",
                "minuto": 5,
                "hora": 13,
                "dia": 4,
                "mes": 12,
                "ano": 23,
                "valor": 6486
            }
        }
    ]
- Reducing the payload

  1. [
        {
            "variable": "segundo",
            "value": 10,
            "metadata": {
                "mqtt_topic": "mensagem/json"
            }
        }
    ]
- Removing the metadata for this case of having reduced the payload:

  1. [
        {
            "variable": "segundo",
            "value": 10  
        }
    ]

- Removing the value (idk, maybe it suddently worked?)

  1. [
        {
            "variable": "segundo",
           
            "metadata": {
                "mqtt_topic": "mensagem/json",        
                "minuto": 5,
                "hora": 13,
                "dia": 4,
                "mes": 12,
                "ano": 23,
                "valor": 6486
            }
        }
    ]
  2. [
        {
            "variable": "segundo",
            "minuto": 5,
            "hora": 13,
            "dia": 4,
            "mes": 12,
            "ano": 23,
            "valor": 6486,
            "metadata": {
                "mqtt_topic": "mensagem/json"
            }
        }
    ]
- Loading an example
 
  1. [
      {
        "variable": "payload",
        "value": "0109611395",
        "metadata": {
          "mqtt_topic": "mensagem/json"
        }
      }
    ]
None of these solutions worked. The error keeps the same. In some cases, it changes to TypeError: Cannot read properties of undefined (reading 'metadata')
but that's in moments like when I had the variables with upper case, such as "Ano" or in the last solution I tried


Please help me!