Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

51
Views
Await fetch not working inside class constructor

This code is working fine except it is not waiting for the fetch to be finished causing undefined references to rendimiento.MesHora after Rendimiento object creation using new instruction.References to other rendimiento properties works fine and

export default class Rendimiento {

constructor(lon, lat, inclinacion, azimuth, perdidas) {

    let fromPVGIS = this.loadRendimiento(lon, lat, inclinacion, azimuth, perdidas);
    this.MesHora = fromPVGIS.rendimientoMesHora;
    this.lon = lon;
    this.lat = lat;
    this.inclinacion = fromPVGIS.inclinacion;
    this.azimuth = fromPVGIS.azimuth;
    this.perdidas = fromPVGIS.perdidas;
};

async loadRendimiento(lon, lat, inclinacion, azimuth, perdidas) {
    const nombreMes = ["Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dic"];

    let _PVGIS = {inclinacion: 37., azimuth: -3., perdidas: 0.14, rendimientoMesHora: Array.from(Array(12), () => new Array(24).fill(0))}; 

    var url = './Datos/PVGIS Original.json';

    var _rendimientoMesHora = Array.from(Array(12), () => new Array(24).fill(0));
    var jsonData;
    var hora;
    var mes;

    try {
        const response = await fetch(url);
        if (response.status === 200) {
            var data = await response.text();
            jsonData = JSON.parse(data);
            for ( hora = 0; hora < 24; hora++) {
                for ( mes=0; mes<12; mes++) {
                    _rendimientoMesHora[mes][hora] = jsonData[hora][nombreMes[mes]] / 1000.;
                }
            }
            _PVGIS.rendimientoMesHora = _rendimientoMesHora;
            return (_PVGIS);
        } else {
            console.log(response);
        }
    } catch (err) {
            console.log(err);
            throw err; 
    };
};

};

where is the mistake?

7 months ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.