0

I am attempting to dynamically populate the dropdown on my template. I am adding company IDs to an array and then attempting to pass the array to the template.

Adding the IDs to the array:

var express = require('express');
//var mysql = require('./dbcon.js');


var mysql = require('mysql');
var pool = mysql.createPool({
  connectionLimit : 10,
  host  : 'localhost',
  user  : 'root',
  password: 'Baseball247!',
  database: 'award'
});


var app = express();
var handlebars = require('express-handlebars').create({defaultLayout:'main'});

app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', 3000);
app.use(express.static('public'));
app.get('/add-user', function(req,res){
  var context = {};
  pool.query("SELECT id from company;", function(err,rows,fields){
    if (err){
      console.log(err);
      next(err);
      return;
    }
    context.results = rows;
    var companies = [];
    //console.log('The solution is: ', rows.length);
    for (var i =0; i < rows.length; i++) {
      //console.log('The solution is: ', rows[i]["id"]);
      companies.push(rows[i]["id"]);
    }
    console.log(companies);
    res.render('addUser', {companies : companies});
  });
});

Then, I try to display those values in the dropdown via:

<select>
    <% for(var i=0; i < companies.length; i++) { %>
        <option><%= companies[i] %></option>
    <% } %>
</select>

However, when I look at the web page the dropdown shows this instead of the company IDs: enter image description here

When I console.log the array I see the correct results so I assume the issue is either with how I am passing the array or how I am traversing/calling the array in the template. Does anyone know what I am missing here?

4
  • Where is the code that actually fills the template using the handlebars library? Is it on the server or client and can you show that code please. Commented Apr 28, 2018 at 23:37
  • @JasonLivesay Maybe I'm confused. My code that I thought was populating my template using handlebars was the first block of code that is running on the server. The first block of code is from my server side code. Commented Apr 28, 2018 at 23:59
  • I dont see any code that references handlebars, where is that Commented Apr 29, 2018 at 0:19
  • @JasonLivesay I added it to the server side code above. Apologies if this is not what you are referencing. Commented Apr 29, 2018 at 0:47

2 Answers 2

1

The syntax for the select should be:

    <p>Company: <select name="company" id="company">
    {{#each companies}}
        <option>{{this}}</option>
    {{/each}}
    </select>
Sign up to request clarification or add additional context in comments.

Comments

0

Handlebars uses curly braces not greater than percent. Can you please check the basic documentation? Thank you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.