-2

I'm trying to change the comments in each post by the number of comments, but The arrays keep the original data when I put in the answer of express.

I tried use another Object and map instead foreach but nothing works.

router.get('/',  function(req, res, next) { 

    postModel.find({}, function(err, posts){
      if(err){
        next(err)
      }else{
        posts.forEach((post,i) => {
          posts[i].comments= post.comments.length;
        });
        res.json({ status: 'success' ,message: "Posts Encontrados!!" , data : {posts: posts}});
      }
    });

  });

New code:

router.get('/',  async function(req, res, next) { //get all posts

var p;

await postModel.find({}, function(err, posts){

  if(err){
    next(err)
  }else{
    p= posts;
    //res.json({ status: 'success' ,message: "Posts Encontrados!!" , data : {posts: posts}});
  }
});
p.forEach((post,i) => {
  p[i].comments= post.comments.length;
});

res.json({ status: 'success' ,message: "Posts Encontrados!!" , data : {posts: p}});

});

  • @GetOffMyLawn He wants to replace each comment with its length, there's nothing about a total here. – Barmar Aug 14 '19 at 20:55
  • I can't see any reason why the code wouldn't work. You can also write `post.comments = post.comments.length;` – Barmar Aug 14 '19 at 20:56
  • thanks, but is still not working... – Oscar Angel Cardenas Aug 20 '19 at 18:55
  • Your code works here: https://jsfiddle.net/barmar/0871amLd/3/ – Barmar Aug 20 '19 at 19:02
  • yes, but in my code is not working, I tried using the foreach out of the find function but i'm still the same result. – Oscar Angel Cardenas Aug 20 '19 at 21:15
  • There must be something more to the problem that you're not showing. Don't forget that `.find()` is asynchronous, you can't access the result outside of the callback function. – Barmar Aug 20 '19 at 21:17
  • But `res.json()` is in the proper place, the data being sent to the client should be correct. – Barmar Aug 20 '19 at 21:18
  • this is the new code: `router.get('/', async function(req, res, next) { //get all posts var p; await postModel.find({}, function(err, posts){ if(err){ next(err) }else{ p= posts; //res.json({ status: 'success' ,message: "Posts Encontrados!!" , data : {posts: posts}}); } }); p.forEach((post,i) => { p[i].comments= post.comments.length; }); res.json({ status: 'success' ,message: "Posts Encontrados!!" , data : {posts: p}}); });` – Oscar Angel Cardenas Aug 20 '19 at 21:19
  • Don't put code in comments, add an update to the question. – Barmar Aug 20 '19 at 21:19
  • What's the difference other than the variable `p`? – Barmar Aug 20 '19 at 21:20
  • could be a context problem but not is the same result. the P variable contents the posts. here the result https://gyazo.com/4183cd73dbb35819a8eaa222f52372f0 – Oscar Angel Cardenas Aug 20 '19 at 21:23
  • The problem isn't variable scope. The problem is that `find()` is asynchronous. See https://stackoverflow.com/questions/23667086/why-is-my-variable-undefined-after-i-modify-it-inside-of-a-function-asynchron – Barmar Aug 20 '19 at 21:27

1 Answers1

1

From MDN:

forEach() does not mutate the array on which it is called (although callback, if invoked, may do so).

If you do need to change the original array, use either.map().

For example:

posts = posts.map(post => {
post.comments = post.comments.length;
return post
}); 

This will transform the original array and return your desired values.

ISAE
  • 519
  • 4
  • 12