I'm trying to wrap my head around this but having some troubles.Basically, I'm first fetching "Search" objects, and then need to fetch "SearchResult" objects using SearchResult.find({searchId: search._id}, function(err, results) { ... });. I'm trying to set it up with callbacks, which for now looks something like this:function retrieveSearches() { searches = []; Search.find({}).sort('-_id').exec(function(err, searches) { if (err) { console.log(err); } callback(searches); }); } function retrieveSearchResults(searches) { search.results = []; searches.forEach(function(search) { SearchResult.find({searchId: search._id}, function(err, results) { if (err) { console.log(err); } else { search.results = results; } }); }) callback(searches); } retrieveSearches(function(searches) { retrieveSearchResults(searches, function(searchesWithResults) { res.render('profile', { title: 'Mine søk', searches: searchesWithResults }); }); }); Of course, retrieveSearchResults won't work because the callback is called before the forEach loop is done. I just don't see how I can loop through an array of objects and perform a single query on each and every one of them, use the result for something and then render.Any ideas?
Submitted November 20, 2019 at 09:41PM by BillGoats
No comments:
Post a Comment