0

I'm thinking it has something to do with the way javascript handles parent/child mutating... but I"m not sure.

The code:

router.get('/:id/articles', authenticate, async (req, res, next) => {
  try {
    let user = await db.getUserIdName(req.params.id);

    if (user) {
      articles = await db.getUserArticles(req.params.id);
      user.articles = articles;

      // select categories.id from `articles_categories_relationship` join `categories` on `articles_categories_relationship`.`article_id` = `categories`.`id` where `articles_categories_relationship`.`category_id` = '1'

      let finalUser = Object.assign({}, user);

      user.articles.forEach(async (article, index) => {
        finalUser.articles[index].categories = [];
        const newCategories = await dbArticles.getCategoriesByArticleId(
          article.id
        );

        newCategories.forEach(async category => {
          await finalUser.articles[index].categories.push(category.id);
        });
        console.log(newCategories);
        console.log(finalUser.articles[index].categories);
      });
      console.log(finalUser);
      // console.log(user.articles[0]);

      res.status(200).json(finalUser);
    } else {
      next({ code: 400 });
    }
  } catch (err) {
    next(err);
  }
});

Here is what the console.log()'s output is... Note: see categories

{ id: 1,
  display_name: 'RandomBlogger',
  articles:
   [ { id: 1,
       user_id: 1,
       cover_page: 'https://coverpage1.com/',
       title: 'Hello World',
       link: 'https://helloworld.com/',
       categories: [] },
     { id: 2,
       user_id: 1,
       cover_page: 'https://i.imgur.com/zbg9mtf.png',
       title: 'Lambda Strikes Down Students With New Build Week',
       link: '',
       categories: [] },
     { id: 3,
       user_id: 1,
       cover_page: '',
       title: 'Deadlines?-?Bad reason for bad code.',
       link: 'https://medium.com/mindorks/deadlines-bad-reason-for-bad-code-d3d5fe22f3ff',
       categories: [] } ] }
::1 - GET /users/1/articles HTTP/1.1 200 526 - 7.632 ms
[ { id: 1 }, { id: 3 } ]
[ 1, 3 ]
[ { id: 2 } ]
[ 2 ]
[ { id: 1 } ]
[ 1 ]

Here is what I expect from the console.log()'s output... Note: see categories

{ id: 1,
  display_name: 'RandomBlogger',
  articles:
   [ { id: 1,
       user_id: 1,
       cover_page: 'https://coverpage1.com/',
       title: 'Hello World',
       link: 'https://helloworld.com/',
       categories: [ 1, 3 ] },
     { id: 2,
       user_id: 1,
       cover_page: 'https://i.imgur.com/zbg9mtf.png',
       title: 'Lambda Strikes Down Students With New Build Week',
       link: '',
       categories: [ 2 ] },
     { id: 3,
       user_id: 1,
       cover_page: '',
       title: 'Deadlines?-?Bad reason for bad code.',
       link: 'https://medium.com/mindorks/deadlines-bad-reason-for-bad-code-d3d5fe22f3ff',
       categories: [ 1 ] } ] }
::1 - GET /users/1/articles HTTP/1.1 200 526 - 7.632 ms
[ { id: 1 }, { id: 3 } ]
[ 1, 3 ]
[ { id: 2 } ]
[ 2 ]
[ { id: 1 } ]
[ 1 ]

If anyone could explain to me why these changes are not being saved after it exits the parent forEach loop, I would be very grateful

Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83
  • Please don't abbreviate JavaScript as "Java" ... – Jonas Wilms Feb 05 '19 at 21:01
  • The code *after* the `forEach` does run *before* the code *inside the `forEach` behind the first `await`* runs, so you actually want to properly wait for the things forEach is doing before continuing, see dupe. – Jonas Wilms Feb 05 '19 at 21:04
  • made the change from "java" to "javascript" (sorry about that), and yeah I just realized the console log is logging out of order, so I added two `await` in front of both `forEach` loops, but that didn't change anything – Fiddle Freak Feb 05 '19 at 21:08
  • please *read the dupe* ... – Jonas Wilms Feb 05 '19 at 21:10
  • I did, so my first forEach loop was already doing that, I added the 2nd async/wait to the 2nd forEach loop. Am I doing that correctly? (I edited the question)... because this is still not working. – Fiddle Freak Feb 05 '19 at 21:13
  • Seriously. Read the dupe! `If you want to read the files in sequence, you cannot use forEach indeed` ... – Jonas Wilms Feb 05 '19 at 21:23
  • okay thanks, I guess i'll change it to a for. – Fiddle Freak Feb 05 '19 at 21:25
  • Just want to say thanks again for help. Making the change worked... this question is indeed a duplicate... but I will post the solution code anyway – Fiddle Freak Feb 05 '19 at 22:10

0 Answers0