【问题标题】:Loop over Array inside Loop循环内循环数组
【发布时间】:2019-02-13 16:26:29
【问题描述】:

基于此Mongoose embedded documents / DocumentsArrays id 我尝试在循环内的循环中实现条件。最后,新的“tosave”评论对象应该包含来自 BlogPost 基于“where a_id = b_id”的_id。

我使用节点、快递和猫鼬。 我有一个包含多个博客帖子的多行表单,一个博客帖子可以有多个评论。

在后端,我有 2 个数组 -> 像这样的博客帖子和评论:

BlogPosts: [{ a_id: '1', name: 'name1' },
            { a_id: '2', name: 'name2' },
            { a_id: '3', name: 'name3' }], 


Comments: [{ b_id: '1', name: 'comment for BlogPost name1' },
           { b_id: '1', name: 'other comment for BlogPost name1' },
           { b_id: '3', name: 'comment for BlogPost name3' }],

我需要每个评论中的博客文章文档中的 _id,以便在评论和博客文章之间进行引用。

我像这样遍历博客帖子:

var new_post_to_save = [];

    for(var i=0; i<req.body.blogposts.length;i++) {

       var newpost = new BlogPost();

       console.log('blogpost nr : ' + [i] + ' has new _id: ' + newpost._id );

       newpost.name = req.body.blogposts[i].name,
       newpost.a_id = req.body.blogposts[i].a_id

       new_post_to_save.push(newpost);

     },

这工作正常。我得到一个新数组“new_post_to_save”,我可以轻松保存。 现在我尝试在 BlogPosts 中循环,为每个评论提供来自新创建的“newpost”对象的 _id 字段。

我尝试了什么 - 但它失败了。它匹配所有博客帖子的所有评论。

var new_post_to_save = [];
var new_comment_to_save = [];

     for(var i=0; i<req.body.blogposts.length;i++) {

       var newpost = new BlogPost();

       console.log('blogpost nr : ' + [i] + ' has new _id: ' + newpost._id );

       newpost.name = req.body.blogposts[i].name,
       newpost.a_id = req.body.blogposts[i].a_id

       // here a loop to create x Comments with the _id from newpost and 
       // the condition: IF a_id = b_id 
       // then create new Comment and get die _id from his Blogpost

       for(var f=0; f<req.body.comments.length;f++) {

         if (newpost.a_id = req.body.comments[f].b_id) 
         console.log(' Yea ! Comment ' + [f] + ' matches Blogpost Nr. ' + [i]);

          var newcomment = new CommentModel();

          newcomment.id_newpost = newpost._id,
          newcomment.name = req.body.comments[f].name,
          newcomment.b_id = req.body.comments[f].b_id,       
        };  // end if
     };   // end for


    new_comment_to_save.push(newcomment);
    new_post_to_save.push(newpost);

      },    // end for

这是我的第一个问题。

有什么想法吗?非常感谢

【问题讨论】:

    标签: arrays loops mongoose


    【解决方案1】:

    我不熟悉Mongoose,只是看你的代码,似乎你的代码有一点错误: position of: new_comment_to_save.push(newcomment);它应该在 if 中,因为只有当它们匹配时,您才希望添加它们..

    var new_post_to_save = [];
    var new_comment_to_save = [];
     for(var i=0; i<req.body.blogposts.length;i++) {
    
       var newpost = new BlogPost();
    
       console.log('blogpost nr : ' + [i] + ' has new _id: ' + newpost._id );
    
       newpost.name = req.body.blogposts[i].name,
       newpost.a_id = req.body.blogposts[i].a_id
    
       // here a loop to create x Comments with the _id from newpost and 
       // the condition: IF a_id = b_id 
       // then create new Comment and get die _id from his Blogpost
    
       for(var f=0; f<req.body.comments.length;f++) {
    
         if (newpost.a_id = req.body.comments[f].b_id) 
         console.log(' Yea ! Comment ' + [f] + ' matches Blogpost Nr. ' + [i]);
    
          var newcomment = new CommentModel();
    
          newcomment.id_newpost = newpost._id,
          newcomment.name = req.body.comments[f].name,
          newcomment.b_id = req.body.comments[f].b_id,
          new_comment_to_save.push(newcomment);       
        };  // end if
     };   // end for
    
    
    
    new_post_to_save.push(newpost);
    
      },    // end for
    

    【讨论】:

    • 感谢您的帮助!就是这个,= 应该是 ==。
    猜你喜欢
    • 2018-01-21
    • 2012-12-22
    • 2014-06-18
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多