My Express.js app has been throwing Cannot read property 'req' of undefined
. In essence, it listens for a GET
request, grab the content
query, and then reply with a table. Here's the parts that present the problem.
index.js
var panels = require('./modules/panels.js');
app.get('/panel', function (req, res) {
var user;
if (user = req.session.user) {
panels.getContents(req.query.content, user.innId, res.send);
} else {
res.sendStatus(401);
}
});
modules/panels.js
exports.getContents = function(panelName, innId, callback) {
var response = "";
switch (panelName) {
case 'tenants':
con.query(queryString, queryParams, function(err, rows) {
if (err) {
handle(err);
} else {
if (rows.length == 0) {
var tenants = 0;
var debtors = 0;
} else {
var tenants = rows[0].tenants;
var debtors = rows[0].length;
}
response = convertToHTMLTable(rows);
}
callback(response); /*THE ERROR POINTS HERE */
});
break;
/* other cases here */
default:
response += "Invalid Request";
callback(response);
}
}
What did I do wrong? My guess is that I'm not suppose to pass res.send
as a callback. So, how can I fix it?