Calling a helper function with a callback from a Jade template

I just started working with Node.js, so please forgive any nonsense !!! I am trying to create a new application using Node.js. Im using an Express structure with a Postgresql database. The problem is that, in my opinion, I wanted to call a function. Therefore, I used a helper function that is called from my jade file. But since this function accesses the database, I tried using callback inorder to make it work.

However, I cannot name the function from my jade template with the last argument as a function. The helper function worked fine when only one parameter was passed, and it was not a callback function. But since the database query took some time, the data was never displayed. But when I try to call a function with a callback from my jade template, I get a syntax error.

My function call in my jade pattern:

#{ nameAndVersion(result.bu_entrep_id, function(error, result)) } 

My helper function (this is simple because I tried to make it work):

exports.helpers= {
nameAndVersion: function(entid, callback) {
var x=1;
     callback(null, x);
     console.log(1);
}
};

My mistake:

500 SyntaxError: Unexpected token )

So basically, I want to call a helper function from my jade template and use this function as a callback function.

+3
source share
1 answer

You want to do:

  • Analysis template
  • To get data

, Express templating :

complexe (, ?).

, .

, , .

app.get('anyPage', function(req, res) {
   database.doSomeDataBaseQuery( /* Data base callback */ function(data, err) {
     if(!err) res.render('pageTemplate', {dataBaseData:data});
   }
});
+3

All Articles