Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with promises inside array.map (Mongoose)

    In mongoose I'm trying to find all threads in my MongoDB database with Thread.find, then with the threads I want to map. In each iteration of map I want to do User.findById and assign the user.name to the thread. I have tried many variations of code but it always seems to return the threads before the map finishes changing their names.

    How would you go about doing this in Mongoose / Express?

    1. Find a collection of objects
    2. For each of the objects, Find a user that matches the Id of the object's "user" property (since the user's ID is added to a thread at the time of creation)
    3. Modify a property of the object based on a property of the user (like add the user name and avatar to the thread object)
    4. Send the modified objects as a response

    The reason I want to do this is because I want the users to be able to change their names and avatars. Then when people request the threads that those user's have posted the threads will reflect those changes.

    My most recent attempt:
    Code:
    router.get("/:forumSection", (req, res) => {
      Thread.find({ forumSection: req.params.forumSection })
        .sort({ date: -1 })
        .then(threads => {
          function nameThreads() {
            return new Promise((resolve, reject) => {
              threads.map(thread => {
                User.findById(thread.user).then(user => (thread.title = user.name));
                console.log(thread);
              });
              resolve();
            });
          }
    
          async function sendThreads() {
            await nameThreads();
            console.log("done");
            res.json(threads);
          }
          sendThreads();
        })
        .catch(err => res.status(404).json({ nothreadfound: "No thread found" }));
    });

  2. #2
    New to the CF scene
    Join Date
    Dec 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb

    After a long time searching, I found this answer: (I put the userís name as the thread title just for testing purposes. Now I will change it to the threadís user name)

    Found the answer in this thread: https://stackoverflow.com/questions/...ript-array-map

    Code:
    router.get("/:forumSection", (req, res) => {
      Thread.find({ forumSection: req.params.forumSection })
        .sort({ date: -1 })
        .then(threads => {
          let promises = threads.map(thread => {
            return User.findById(thread.user).then(user => {
              thread.title = user.name;
              return thread;
            });
          });
          Promise.all(promises).then(threads => {
            res.json(threads);
          });
        })
        .catch(err => res.status(404).json({ nothreadfound: "No thread found" }));
    });


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •