0

I am running a server on node.js. When I look at the result inside Google Chrome by typing localhost:3030 it returns the value [object Object].

this is the app.js code here:

    var express = require('express');
    var mysql = require('mysql');
    var app = express();
    app.set("view engine", "ejs");

    var connection = mysql.createConnection({
      host: 'localhost',
      user: 'abcdefg',
      password: 'password',
      database: 'test'
    });

    app.get("/", function(req,res){
      var q = "select * from showdata";
      connection.query(q, function(err, results){
        if(err) throw err;
        var products = results;
        res.render("home", {data: products});
      });
    });

    app.listen(3030, function(){
      console.log("server running on 3030");
    })

and this is the .ejs file:

    <h1>Test</h1>
    <p> <strong><%=data%></strong></p>

No errors show up. It just says [object Object], whereas it should return all values from the query.

Philosophist
  • 103
  • 1
  • 13

2 Answers2

0

Try this:

<h1>Test</h1>
<% data.forEach(function(item) { %>

    <p> <strong><%= item %></strong></p>

<% }); %>
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
0

pmkro helped with linking the correct website: Loop through JSON in EJS This is the code I used:

<h1>Test</h1>
<% data.forEach(function(item) { %>

    <p> <strong><%= item %></strong></p>

<% }); %>
Philosophist
  • 103
  • 1
  • 13