question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I have a Node.JS application that is hosted on Ubuntu 16.04. I am using EJS to display queried data from MySQL but I am getting a reference error like this

ReferenceError: /home/michaeluy/apps/michaeluy.me/views/index.html:130
 128| </div><br/><br/>
 129|         
  >> 130| <%=sec%>
  131| </div>
 132| </div>
 133| </body>

 sec is not defined
 at eval (eval at compile (/home/michaeluy/apps/michaeluy.me/node_modules/ejs/lib/ejs.js:485:12), <anonymous>:11:23)
 at returnedFn (/home/michaeluy/apps/michaeluy.me/node_modules/ejs/lib/ejs.js:514:17)
 at View.exports.renderFile [as engine] (/home/michaeluy/apps/michaeluy.me/node_modules/ejs/lib/ejs.js:358:31)
 at View.render (/home/michaeluy/apps/michaeluy.me/node_modules/express/lib/view.js:126:8)
 at tryRender (/home/michaeluy/apps/michaeluy.me/node_modules/express/lib/application.js:639:10)
 at EventEmitter.render (/home/michaeluy/apps/michaeluy.me/node_modules/express/lib/application.js:591:3)
 at ServerResponse.render (/home/michaeluy/apps/michaeluy.me/node_modules/express/lib/response.js:961:7)
 at /home/michaeluy/apps/michaeluy.me/server.js:15:9
 at Layer.handle [as handle_request] (/home/michaeluy/apps/michaeluy.me/node_modules/express/lib/router/layer.js:95:5)
 at next (/home/michaeluy/apps/michaeluy.me/node_modules/express/lib/router/route.js:131:13)`

and here the first bit of my server.js file

var http = require('http');
 var express = require('express');
var fs = require('fs');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var connection  = require('express-myconnection');
var app = express();

app.use('/',express.static(__dirname + '/'));
app.use(bodyParser.urlencoded({ extended: false }));
app.engine('.html', require('ejs').__express);
app.set('view engine','html');

var connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: '',
    database: 'michaeluy'
});

 app.get('/', function(req, res){
    connection.query("SELECT * FROM messages", function(err, rows, fields){
        if (err) throw err;
        res.render('index', {
            sec: rows[0].name
        });
    });
 });

I have tried using console.log at first to see if it is able to query the data correctly and it did but when I set the code on the html file for it to show up on the page itself. It gives back a reference error.

Hope someone can help here. Thanks in advance`

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
MichaelUyycommented, Nov 25, 2016

@RyanZim I am not entirely sure regarding the database issue since I am able to query data as long as it is outside any of the app.get’s. Anyways, thanks for the help. I’ll try to see what I can do

1reaction
mdecommented, Nov 24, 2016

Maybe it’s the way you’ve set up the view engine.

I have a small Express app (v4.13.4) with EJS (v2.5.2), and this code words correctly:

'use strict';

let express = require('express');
let app = express();

// Serve static files out of /public
app.use(express.static('public'));
app.set('view engine', 'ejs');

app.get('/foo', function (req, res) {
  res.render('foo', {
    bar: 'BAR'
  });
});

app.listen(8000, function () {
  console.log('Example app listening on port 8000!');
});

When I hit http://localhost:8000/foo, I see the “Howdy BAR” output as expected. I have a foo.ejs file in the “views” directory in the app.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError - JavaScript - MDN Web Docs
The ReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is ...
Read more >
How to Resolve an Uncaught Reference Error in Javascript
The Javascript ReferenceError occurs when referencing a variable that does not exist or has not yet been initialized in the current scope.
Read more >
JavaScript ReferenceError: A Complete Guide | Career Karma
In JavaScript a reference error is mainly thrown when a code is attempting to reference a variable that does not exist, but there...
Read more >
How to correct a #REF! error - Microsoft Support
The #REF! error shows when a formula refers to a cell that's not valid. This happens most often when cells that were referenced...
Read more >
JavaScript Reference Errors - Meticulous
In essence, a reference error occurs when JavaScript tries to access a variable that doesn't exist, hasn't been defined, or doesn't exist in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found