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

272
Views
how to pass data from node.js to local javascript, keeping array capabilities?

how to pass data from node.js to local javascript, keeping array capabilities?

const query = db.query(sql, post, (err, results) => {
        if (err) {
            throw err;
        } else if (results.length >= 1) {
            
            console.log(results);
            const listResults = [];

            for (let i = 0; i < results.length; i++) {
                
                listResults.push(results[i].ENwords);
                listResults.push(results[i].PLwords);
            }
            
            res.render('flashcards', {layout: 'flashcards', listResults});

in flashcard.ejs:

<script>

let listWords = <%= listResults %>;
console.log(listWords);

</script>

I get an error that the first item in the list is "is not defined".

I've also tried this. This works but the value is no longer an array:

<script>

let listWords = "<%= listResults %>";
console.log(listWords);

</script>

In this particular case, reworking these values ​​is pointless, because different data are stored there and it is not possible to divide them in an appropriate way to make a new list.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Stringify the array in Node.js

let listWords = <%- JSON.stringify(listResults) %>;

JSON.stringify creates a JSON representation of the string and JSON can be used as array literal.

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!