I've just started working with Node.js so please forgive any stupidity!!! I'm trying to create a new application using Node.js. Im using the Express framework with a Postgresql database. The problem is that in my view, I wanted to to call a function. So I used a helper function that is called from my jade file. But because this function accesses the database, I tried to use callback inorder to make it work.
However, I can't seem to call a function from my jade template with the last argument as a function. The helper function worked fine when there was only one parameter being passed and it wasn't a callback function. But because the database query took a while, the the data was never displayed. But when I try to call a function with callback from my jade template, I get a syntax error.
My function call in my jade template:
#{ nameAndVersion(result.bu_entrep_id, function(error, result)) }
My helper function (It's simple because I was trying to get it to work):
exports.helpers= {
nameAndVersion: function(entid, callback) {
var x=1;
callback(null, x);
console.log(1);
}
};
My error:
500 SyntaxError: Unexpected token )
So, basically, I want to call a helper function from my jade template and have that function be a callback function.
You want to do :
Express templating is expected to do :
You should not have to execute complexe code once you've started rendering (what if you database is unavailable ?).
Jade helpers only have a formating purpose, not functionnal.
What you should do, instead of calling an helper, is giving the necessary data when calling the renderer.
app.get('anyPage', function(req, res) {
database.doSomeDataBaseQuery( /* Data base callback */ function(data, err) {
if(!err) res.render('pageTemplate', {dataBaseData:data});
}
});