Retrieve arguments from a javascript array

Asked

Viewed 21 times

0

I have a page that returns the data as follows:

"test,123change,IMPEXPROS,Sarmento:172.20.1.1,172.20.1.2,172.20.1.3,172.20.1.4,172.20.1.5,172.20.1.6,255.255.255.0,192.168.1.1,192.168.1.140:"

am making another page to handle this data. First I receive the data with this function:

const getDados = async () => {
  let url = 'http://' + host + '/dados';
  xhttp.onreadystatechange = () => {
      if (xhttp.readyState == 4 && xhttp.status == 200) {
          var data = xhttp.responseText;
          prepareListDados(data);
      }
  }
  await xhttp.open("GET", url, true);
  await xhttp.send();
}

After I take the data I separate them this way:

const prepareListDados = (dados) => {
  let arrParams = dados.split(':');
  let nomes = arrParams[0].split(',');
  let ips = arrParams[1].split(',');
  let arrDados = [];

  arrDados.push({
    ssidAP: nomes[0],
    senhaAP: nomes[1],
    ssidSTA: nomes[2],
    senhaSTA: nomes[3],
    ipLocal: ips[0],
    ipmodulo1: ips[1],
    ipmodulo2: ips[2],
    ipmodulo3: ips[3],
    ipmodulo4: ips[4],
    ipmodulo5: ips[5],
    mascaraAP: ips[6],
    gatewaySTA: ips[7],
    localSTA: ips[8],
    mascaraSTA: ips[5],
    servidorSTA: ips[5],
  });
  texto1(arrDados);
  texto2(arrDados);
}

after that I send the array to a function and wanted to manipulate the values of this arrDados array. I put this way but it is not working:

const texto1 = (arrDados) =>{
  let a = setSSIDAP();
  let b = setSSIDSTA();

  if (a == "null" && b == "null"){
    return "null";
  }
  if(b == "null" && a != "null"){
    return a + ","+ arrDados.ssidSTA + "," + arrDados.senhaSTA;
  }
  if(a == "null" && b != "null"){
    return arrDados.ssidAP + "," + arrDados.senhaAP + "," + b;
  }
  if (a != "null" && b != "null"){
    return a + b;
  }
}

Referring to the following error:

Unhandled Promise Rejection: Typeerror: Undefined is not an Object (evaluating 'arrDados.ssidSTA')

I do not know if I am separating the values correctly, because you can notice that they are separated by "," and separated by blocks ":"

Functions setSSIDAP and setSSIDSTA:

const setSSIDAP = () => {
    event.preventDefault();
    let chk = document.getElementById('bloco1');
    if (chk.checked) {
        if (validateForms('redeInterna')) {
            let localreles = getSSIDS('redeInterna');
            return localreles;
        }
    } else {
        return 'null';
    }
}
const setSSIDSTA = () => {
    event.preventDefault();
    let chk = document.getElementById('bloco2');
    if (chk.checked) {
        if (validateForms('staWifi')) {
            let localreles = getSSIDS('staWifi');
            return localreles;
        }
    } else {
        return 'null';
    }
}
const getSSIDS = (formId) => {
  let frm = document.getElementById(formId);
  let txt = "";
  for (let i = 0; i < 2; i++) {
      txt = txt + frm.elements[i].value + ",";
  }
  let obj = txt.substr(0, txt.length - 1); // remove ',' final da string e add ">"
  return obj;
}
  • your arrDados is an array, it won’t work the way it is. Send using texto1(arrDados[0]); and see if it resolves

1 answer

0

You create an object with the data, insert it into an array of 1 element and pass this array to the functions. That is, the array is totally unnecessary. Directly pass the object with the data.

let dados = {
    ssidAP: nomes[0],
    senhaAP: nomes[1],
    ssidSTA: nomes[2],
    senhaSTA: nomes[3],
    ipLocal: ips[0],
    ipmodulo1: ips[1],
    ipmodulo2: ips[2],
    ipmodulo3: ips[3],
    ipmodulo4: ips[4],
    ipmodulo5: ips[5],
    mascaraAP: ips[6],
    gatewaySTA: ips[7],
    localSTA: ips[8],
    mascaraSTA: ips[5],
    servidorSTA: ips[5],
};

texto1(dados);
texto2(dados);
  • and receive the values with.ssidAP data?

  • @Vitorpereira Yes.

  • continues the same error..

  • If you replaced the unnecessary code with the q code I showed in the reply, and renamed the parameter received by the functions text1 and text2, there is no way to give that error.

  • I did exactly what you said, and the following error happens: Unhandled Promise Rejection: Typeerror: Undefined is not an Object (evaluating 'data2.ssidSTA')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.