I'm trying to get a recordset from my database and store it in a variable so I can use it in a subsequent callback that renders a view. I've tried creating a variable on the req object, but it comes back as undefined when I call it in the next callback.
app.get(
'/behaviorchart',
authenticated,
function(req, res, next) {
sql.connect(dbconfig, function (err) {
new sql.Request()
.input('TeacherID', sql.Int, req.user.UserID)
.execute('dbo.GetTeacherStudentCards', function (err, recordsets, returnValue) {
req.studentCards = recordsets[0] // recordset without return value
});
sql.on('error', function (err) {
// ... error handler
});
});
next();
},
function(req, res) {
console.log(req.studentCards);
res.render('behaviorchart.ejs', {
user : req.user // gets user from session for ejs template
});
}
);
I've read that some people, especially when they're using the variable in their view, create a property on res.locals, but I can't seem to get this to work either.
What am I doing wrong here?