Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
Can I add some variables to my chart.js in page know that variables?

This is my callback function and i have getData(data); to get all JSON data , And created dataset = [ ]; (for get some specific properties from my all JSON data)

function call_some_api(getData){
    request('https://someUrl..........',{json:true},(err,res,data) => {
        if (err) {
            console.log(err);
        }     
        if (res.statusCode === 200){
        
             var dataset = [];

             data.forEach((value, key)=>{
             dataset.push(value.close);

            });                
           getData(data);     
        }
    });
};

And use callback and pass my all JSON data in res.render it works normally. But i want add dataset variable from my callback into res.render to page know variable dataset

app.get('/chart',function(req,res){
    call_some_api(function(getAPI_data){
       
        res.render('chart',{
            dataChart: getAPI_data,
            
        });
    });
});

I just need to my /chart page (handlebars template) know my variable from callback function. (for build chart) This my /chart (handlebars template)

<canvas id="myChart" width="400" height="400"></canvas>
<br><br>

<script>

const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ['Jan', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
        datasets: [{
            label: '# of Votes',
 
           // I just want add in this line below.
            data: dataset,
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255, 99, 132, 1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            y: {
                stacked: true
            }
        }
    }
});

</script>

This error enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You mean like a second argument, analog to getAPI_data ?

function call_some_api(getData){
    request('https://someUrl..........',{json:true},(err,res,data) => {
        if (err) {
            console.log(err);
        }     
        if (res.statusCode === 200){
        
             var dataset = [];

             // BTW this can be better/cleaner done with `.map` ;)
             data.forEach((value, key)=>{
             dataset.push(value.close);

            });                

           getData(data, dataset);   
  
        }
    });
};

And in your render/http endpoint

app.get('/chart',function(req,res){
    call_some_api(function(getAPI_data, dataset){
       
        res.render('chart',{
            dataChart: getAPI_data,
            dataset
        });

    });
});

NOTE: Code not tested, inline edited.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda