Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

314
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!