I am trying to access some DB results with Node/Express.js on Parse.com because I have some additional data to loop next. Basically I need the Navigation table to loop, and then inside that loop I will loop another table. Is this the proper way? I cannot access the variable navigation
outside of method below:
var Navigation = Parse.Object.extend('Navigation');
var navigation;
var query = new Parse.Query(Navigation);
query.ascending('SortOrder');
query.find().then(function(results) {
navigation = results
}, function() {
res.send(500, 'Failed loading navigation');
});
res.render('index', { nav: navigation });
Thoughts? I am basically building a mobile single page app. I don't really need routes. But I want to loop through and build the navigation. And also loop through and build the other DIV's which will be shown/hidden according to what the user selects from the navigation. Hope that makes sense.
As currently written, navigation
is being assigned a value during a callback from an async function...AFTER it is passed to the renderer. At the time that render
is called, navigation
has been declared, but not yet given a value.
Any time you assign a value as part of an async callback, every operation that you wish to make use of that value has to either be part of the callback or happen after it (for instance, in a subsequent chained .then()
)
In this case, you can move your render
call into the callback and that should be sufficient.
The problem is res.render
is called before the callback from query.find
. Try Moving res.render('index', { nav: navigation });
inside of query.find().then(function(results) {
navigation = results
}