Hey, r/Node, I am building a MERN stack (using Mongoose) blog type of site and I am working on my API right now. I am building it so users can sign up and bookmark/favorite different posts sort of like how you would on Medium.com. If the user is logged in and goes to /favorites it'll bring up a page of all of their favorites.The flow in my head is working like this but I can't seem to get it working in code with promises/async await.send userID (aka _id) to the endpoint as a param in the URLendpoint captures this via req.param.userIDfunction queries for this user ID in the user collection and captures the array of favorited post IDsfunction takes this array of post IDs and queries the posts collection to return an array of documents matching these IDssend this array back to React to be displayed to the userThe problem is I have no clue the best way to solve this. My current solution to prevent prematurely firing off res.send(favorites) with an empty array (due to asynchronous code) is to place the res.send(favorites) in an if statement to check if the posts array matches the length of the user's favorites array of post IDs haha in other wordsif (postsArray.length === user.favorites.length) { res.send(postsArray)` } I know this is beyond bad form but it works for now but I'd like to avoid ending up on r/badcode. I'd really like to use async/await if that's the best solution. But I'm really confused as to how I should apply it here or if I even need to if Mongoose/MongoDB has a better solution for this? What is the best way to find every document in a collection that matches an array of _id's?This is my code. Please go easy on me, I'm at my wits end here haha.userController.getFavorites = (req, res) => { let favorites = []; let listings = []; const user = req.params.userID; if (true) { // this is set to true for testing in postman but will be checking for user authentication in production User.findById(user).exec((err, user) => { favorites = favorites.concat(user.favorites); for (let i = 0; i < favorites.length; i++) { Place.findById(favorites[i]).exec((err, favorite) => { listings = listings.concat(favorite); // this conditional is making sure the res.send doesn't get fired off out of order. if (listings.length == favorites.length) { res.send(listings); } }) } }); } } Should I be using promises/async await? Does MongoDB/Mongoose have built-in methods for achieving what I'm trying to do? I can't imagine this "problem" is unique to me. I have tried reading the MongoDB documentation, reading/watching tutorials etc etc but this is the only functional solution I can get working.I appreciate any help or advice you can give me.
Submitted November 16, 2018 at 02:07AM by evsoul
No comments:
Post a Comment