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

316
Vistas
pass Node JS variable to javascript

I am trying to show the entire DB file on the client side.

I have a DB file and it is connected from python script and the python script is connected with my Node JS server. I would like to make a table on html file using javascript. I am not sure this is a proper way to pass a Node JS variable to javascript.

When I do console.log(data[0]); in the javascript file, I thought it is supposed to print out the first line of the data variable but it only prints out the first letter of the entire data variable.

I want to receive the data variable as an object or array from javascript, not as letters, but I do not know how to.

Below is my part of server.js

app.get('/data', function(req, res) {
    var options = {
        mode: 'json',
        pythonPath:'',  
        pythonOptions:['-u'],
        scriptPath:'',
        args: []
    };
    PythonShell.PythonShell.run('dbShow.py', options, function(err, results) {
        if(err) throw err;
        const headings = ['Name','SSN','State'];
        res.render(__dirname + '/data.html',{headings:headings,data:results[0]});
    });
});

Below is my part of javascript code inside of html file.

<script>
    var data = "<%= data %>";
    var headings = "<%= headings %>";
    document.write("<table>");
    for(let i=0;i<data.length;i++) {
        document.write("<tr>");
        for(let j=0;j<headings.length;j++) {
            document.write("<td>"+data[i][j]+"</td>");
        }
        document.write("</tr>");
    }
    document.write("</table>");
</script>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This sets data (client-side) to a string literal:

var data = "<%= data %>";

If the server-side data variable is known to be a JavaScript object or array, and wrapping it in quotes effectively makes it JSON, don't wrap it in quotes:

var data = <%= data %>;

Or parse the JSON

var data = JSON.parse("<%= data %>");

Which could be useful if the server-side data might be empty for example. Or could at least produce a more meaningful error message for such cases rather than a JavaScript syntax error.

But as part of your debugging effort, the very first thing you'll want to do is look at the client-side page source and see what these lines are actually outputting. If it's not valid JavaScript or JSON, you're going to want to address that.

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