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

317
Visualizações
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 Respostas
Responde à pergunta

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 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