2

I want to put the fallowing in a helper but i get and require it in my app.js. My current error is app is not defined. I am new to node.js so if this is a easy one dont be to hard on me.

app.locals.use({flashMessages: function(req, res) {
var html = ""
  , flash  = req.flash();
['error', 'info'].forEach(function(type) {
  if(flash[type]) {
    flash[type].forEach(function(message) {
      html += "<div class='alert " + type + "'>" + message + "</div>";
    });
  }
});
return html; }});
Vartan Arabyan
  • 2,925
  • 7
  • 25
  • 35

1 Answers1

2

You should either convert this to a function that accepts an app parameter, or you could put app into GLOBAL. See node.js global variables?

Community
  • 1
  • 1
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • that was not clear at all. I have no understanding of how to do or what to do with what you have said. – Vartan Arabyan May 23 '12 at 17:29
  • @Vartan Arabyan your problem is that when you move this code to a new file you are losing the reference to app. So what I'm saying is that you can either convert this code into a function that you could call in a context where app is known, or you could put app into a global variable, so that in the context of your new file you could still access app – ControlAltDel May 23 '12 at 17:32
  • can you please give me an example of making it a function. – Vartan Arabyan May 23 '12 at 17:39
  • Well just surround your code with function my_code() {};,place that function in the same file where app is defined and call the function (my_code();) after you define app. – Zlatko Nov 06 '12 at 10:58
  • of course, that's very bad practice, but the most straight forward. – Zlatko Nov 06 '12 at 10:58