So, i'm using express locals to create custom function. I have this:
Menu.getMenu(function(err, menu) {
app.locals.buildMenuStructure = function(){
var ids = [];
for (var i = 0; i < menu.length; i++) {
ids.push(menu[i]._id);
}
return ids;
};
});
But the problem is that when i add some data to Menu collection the function doesn't know about menu being populated. To see new ids i need to restart the server.
Now i realized that i need to retrieve menus inside buildMenuStructure function to see effect immediately. In that case i need to get the value of getMenu asynchronous function and return it. Something like this:
app.locals.buildMenuStructure = function(){
var ids = [];
Menu.getMenu(function(err, menu) {
for (var i = 0; i < menu.length; i++) {
ids.push(menu[i]._id);
}
});
return ids;
}
I plan to use async library for that but i can't make it work((
Note! This functions are simplified.
Menu.getMenu function is the same as mongoose Menu.find({});
Thanks for your answers.