Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
Javascript - Object stops being recognized as a defined object

I'm working on a weather app that pulls some data.

Partway through it stops recognizing one of the objects as an object and throws: Uncaught (in promise) TypeError: data is undefined

Now, this seems to be tied in with my nested if. If I remove the lines indicated,it runs fine and the promise completes. Otherwise it throws the error.

This is the script that stopped working.

async function processHourly(hourly){
  let data = await pullData(hourly); // Pull data is just fetch.then(return res.json)
  data = data.properties;
  let dataHours = data.periods; // Exception thrown on data, here.
  let weatherObj = new Array;
  let objNum=0;
  let avgTemp;

  let dateCheck;
  for(x in dataHours)
  {
    // Grab the day's hours
    //console.log(dataHours[x].startTime)


    if(dataHours[x].startTime.slice(8,10)!=dateCheck){
      weatherObj[objNum] = new Weather
      weatherObj[objNum].Year = dataHours[x].startTime.slice(0,4) // takes 0-3
      weatherObj[objNum].Month = dataHours[x].startTime.slice(5,7)-1 // takes 5 and 6
      weatherObj[objNum].Date = dataHours[x].startTime.slice(8,10) // takes 8 and 9
      weatherObj[objNum].getDay();
      objNum++;
      typeof(weatherObj[objNum])                    // If these are removed
      if(dataHours[x].startTime.slice(11,13)>=12)   // Then it processes data as
      {                                             // a completed promise.
      weatherObj[objNum].IsNight = true;            // 
      }                                             //
    }



    dateCheck=dataHours[x].startTime.slice(8,10);
  }
  console.log(dataHours)  
  for(x in weatherObj){
    weatherObj[x].setDateObj()
    console.log(x,weatherObj[x].dateObj)
  }
}

Anyone know why that is? I feel like I'm missing something pretty simple. It doesn't look like the variables affect data's scope?

edit -1/7/22, Clarified pullData function

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The Project

This is a simple weather app which will be playing on a screen on loop for the community. It pulls data from the weather.gov api's hourly forecasts and processes some data. This is the process being performed.

data's json scheme can be found here


While I'm not entirely certain why, it appears that using = to assign the value was causing issues rather than using array functions.

The below is a shortening of the original highlighting where my issue lies.

Broken Code

if(dataHours[x].startTime.slice(8,10)!=dateCheck){
      weatherObj[objNum] = new Weather /** THIS IS OUR PROBLEM CHILD **/
      ...
      ...
      if(dataHours[x].startTime.slice(11,13)>=12)   // Then it processes data as
      {                                             // a completed promise.
      weatherObj[objNum].IsNight = true;            // 
      }                                             //
    }

Corrections

I instead used .push() to assign the new weather object to the spot. Following up by assigning objNum to equal the index of the current weather object as it iterates through creation.

I additionally added in keys for the hour of the forecast for later processing and averaging.

The correction that I ended up with is as follows.

Working Code


let checkable; //Good for testing
async function processHourly(hourly)
{
  let data = await pullData(hourly);
  data = data.properties;
  let dataHours = data.periods;
  let weatherObj = new Array;
  let objNum=0;

  let dateCheck=-1; // forces first pass to always create a new weather

  console.log(dataHours[0]) // To double check variable names

  for(x in dataHours){ // Start creating objects
    if( dataHours[x].startTime.slice(8,10)!=dateCheck){
      weatherObj.push(new Weather);
      dateCheck = dataHours[x].startTime.slice(8,10);
      objNum = weatherObj.length-1
    }

    weatherObj[objNum].hourTemp.push({
      hour : dataHours[x].startTime.slice(11,13),
      temp : dataHours[x].temperature
    });
    
    weatherObj[objNum].hourForecast.push({
      hour      : dataHours[x].startTime.slice(11,13),
      forecast  : dataHours[x].shortForecast
    })  

    weatherObj[objNum].hourWindDir.push({
      hour      : dataHours[x].startTime.slice(11,13),
      direction : dataHours[x].windDirection
    })
    
    weatherObj[objNum].hourWindspeed.push({
      hour      : dataHours[x].startTime.slice(11,13),
      speed     : dataHours[x].windSpeed.slice(0,-4)
    })
  }
  checkable = weatherObj
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda