Jason villa
Jason villa

Reputation: 37

Node.js Display mysql query with ejs

I can't seem to figure out how the exact syntax to display data I have from a a database. I have 1 row in my user database which has a Password, Email, Username field but I can't seem to display it. I just get a blank screen

I've added the function and ejs file that I'm trying to fugure out. it displays perfectly in the console

//controller.js

module.exports = function(app){
app.get('/users', function(req,res){
    let sql = "Select * from users";
    let query = db.query(sql, (err,result) => {
        if(err){
            console.log(err);
        }
        console.log(result);
        res.render('users', {user: result});

    }); 
});

}

ejs file

<html>
  <head>
    <title>Users List</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script></script>
    <link href = "/assets/style.css" rel="stylesheet" type ="text/css" />
</head>
<body>
    <div id ="todo-table">
        <li><%= player.Email %></li>
    </div>
  </body>
</html>

Upvotes: 1

Views: 1034

Answers (1)

Mark S.
Mark S.

Reputation: 4019

The mysql driver returns results in an array, even if there is only a single row returned. So you might need to return result[0] to get the single row.

Upvotes: 1

Related Questions