I am currently working on retrieving data from a database and passing it to a function. The main issue I'm facing is that even though the query results are visible when logged, they appear as undefined when trying to access them through the function. This could be due to my async function setup with mongoose not being configured properly. Another possibility is that my understanding of asynchronous programming is limited, as I'm relatively new to this concept.
async function returnBlogThumbnails(filter = "recent", callback){
console.log("returning blogs")
//For now simply filter by most recent
if(filter === "recent"){
Blog.find({}).sort('-date').exec((err,docs) => {
return docs;
});
}
}
Here is the code snippet for the function that calls the above function:
app.get('/', (req, res)=> {
console.log("go home");
//Invoke the query to database and then use an async function to return results
database.returnBlogThumbnails().then((blogs) => {
console.log(blogs);
//res.render('home', blogs);
});
});
While the console log displays the desired output, the calling function always returns undefined. Any insights or suggestions would be greatly appreciated. Thank you!