2

I have a simple app that I am building with a friend. We want to take make a dropdown multiselect to have a list that is populated from our DB.

I can get the results no problem, and log them in the console just fine, but I am not able to get the results to pop in the dropdown list.

I've added the code below. The app just shows a blank dropdown list, which looks like the size of the returned values, I just can't tell.

enter image description here

Thanks!

server.js

const express = require('express');
//const ejs = require('ejs');
let mysql = require('mysql2');
let app = express();

let conn = mysql.createConnection({
    host: '***',
    user: '***',
    password: '***',
    database: 'symptom_list'
})

let sql = `SELECT field1 FROM SymptomList`;
let symptoms = [];


app.use(express.static('public'));
app.use(express.urlencoded({
    extended: true
})
);

app.set('view engine', 'ejs');

app.listen(8000);

app.get('/', function (req, res) {
    conn.query(sql, (error, results, fields) => {
        if(error) {  return console.error(error.message) };
            for (let i = 0; i < results.length; i++){
                symptoms.push(results[i].field1);
                console.log(symptoms);
            }
        //console.log(results);
        //symptoms.push(results);
        res.render('pages/index', { dropVals: symptoms[0] });
    });
});

index.ejs

<div>
    <label>Symptoms</label>
    <select>
        <% for (let i=0; i < dropVals.length; i++) { %>
            <option> <% dropVals[i] %></option>
        <% } %>
    </select>
</div>
2
  • try changing: dropVals: symptoms[0] => dropVals: symptoms Commented Jul 29, 2022 at 18:49
  • 1
    @traynor I should have edited the code after I posted.. that was my last ditch attempt to get it to work.. res.render('pages/index', { dropVals: symptoms }); was one of the first things I tried, thinking I was getting into an array that didn't exist. Thanks for your reply though! Commented Jul 29, 2022 at 21:01

1 Answer 1

1

you need to use <%= tags to output value into the HTML:

try changing

    <option> <% dropVals[i] %></option>

to

    <option> <%= dropVals[i] %></option>
Sign up to request clarification or add additional context in comments.

1 Comment

Yup, that was it! I knew it had to be something small, I just wasn't seeing it! Thanks!

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.