【问题标题】:stop ForEach loop from running code 5 times per item每个项目停止 ForEach 循环 5 次运行代码
【发布时间】:2018-05-12 23:39:12
【问题描述】:

我希望我的代码为循环中的每个项目插入 1 个文档,但它会继续插入每个文档中的 5 个。我已经在这几个小时了,现在是凌晨 3 点 41 分,请帮忙。

代码杂乱无章,简单地说,循环将扫描类别列表并使用该类别的索引来提取与之相关的其他数据,例如该人在该类别中获得了多少分,在哪一天等.

我正在使用 nodejs,您将在下面看到的是我在 router.js 文件中调用的架构之一的方法。

是的,我在这里阅读了超过 15 个问题,我认为这可能会有所帮助,但对我使用的编码语言没有帮助,也没有确切的问题。

for 循环构建了我试图保存在学生个人资料中的点对象。

    //the for loop should build something like this
            { date: '9/13/2017',
              category:"Participation",
              specification:"Pitchball Game",
              points:150
            }

js文件:

    StudentSchema.statics.givePoints = function (pt_obj, cb){
var leng =pt_obj.category.length;
var i=0;
var rsc = 'r' + pt_obj.room + '-' + pt_obj.seat;
Student.findOne( {rscombo: rsc },  function (err, student){
    if ( err || !student){
        console.log('not a student');
    }
    else{

        pt_obj.category.forEach(function( cat, ind){

            if(cat != '' && cat != null && cat != undefined && cat != '--select--'){
                var pts = Number(pt_obj.pts[ind]);

                student.points.push({ date: pt_obj.date[ind], category: cat, specification : pt_obj.specification[ind], points:Number(pt_obj.pts[ind]), sysdate:pt_obj.date[ind] });

                if(cat != 'Purchase'){


                    ptMonth = Number(pt_obj.date[ind].slice( 0, pt_obj.date[ind].indexOf('/') ) );

                    var query ={starlight:pts, balance:pts};
                    var trailMe  = 'lightTrails' + ptMonth + '.';
                    var trailCat;
                    var hightrail= trailMe + '0';
                    query[hightrail]=pts;


                    switch (cat){
                        case 'Behavior':
                            trailCat= trailMe + '1';
                            query[trailCat]=pts;
                            console.log(trailCat);
                            break;
                        case 'Contest':
                            trailCat= trailMe + '2';
                            query[trailCat]=pts;
                            console.log(trailCat);
                            break;
                        case 'Game':
                            trailCat= trailMe + '3';
                            query[trailCat]=pts;
                            console.log(trailCat);
                            break;
                        case 'High Score':
                            trailCat= trailMe + '4';
                            query[trailCat]=pts;
                            console.log(trailCat);
                            break;
                        case 'Participation':
                            trailCat= trailMe + '5';
                            query[trailCat]=pts;
                            console.log(trailCat);
                            break;
                        case 'Wage':
                            trailCat= trailMe + '6';
                            query[trailCat]=pts;
                            console.log(trailCat);
                            break;
                    }


                    console.log('attacking query \n' + query);


                    //always add it to the starlight, highlight as well as specific category
                    Student.update( { rscombo : rsc } , { $inc: query }, function (err, student){
                        if(err || !student){
                            console.log('update unsuccessful');
                        } });
                } else{
                    var pts = Number(pt_obj.pts[ind]);
                    Student.update( { rscombo : rsc } , { $inc: { balance: pts} }, function (err){
                        if(err){
                            console.log('update unsuccessful');
                        }
                    } );
                }
            }
            if( leng-1 == i){
                student.save(cb);
            }else{
                student.save(function(err){
                    if(err){
                        console.log(err);
                    }
                })
            }
            i++;
        });
    }
});
return Student.findOne( { rscombo: 'r' + pt_obj.room + '-' + pt_obj.seat},  cb);

}

【问题讨论】:

  • 嗨..据我了解阅读您的代码,您想为每个类别保存一个学生吗?
  • 否 我想将积分信息保存到学生的积分表中。为了做到这一点,我需要 for each 循环来构建点对象。我将编辑帖子以说明我的意思。
  • 那么问题出在哪一行?
  • 就在您看到 for each 循环的位置。它不断将相同的点数据放入 5 次。有一条线我将数据推送到数据库中,这就是我认为我出错的地方。

标签: javascript arrays node.js foreach


【解决方案1】:

我认为你必须在循环中构建点对象,但在循环结束后推送它。

【讨论】:

    【解决方案2】:

    感谢 Amor,您所说的让我开始思考,我切换了代码,以便我的 forEach 位于外部,而 findOne 方法位于内部。它似乎奏效了。非常感谢。

      pt_obj.category.forEach(cat, ind){ 
        Student.findOne({}, function (){
    
        })
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2011-03-29
      • 1970-01-01
      • 2020-02-22
      • 2012-07-12
      • 2018-10-28
      相关资源
      最近更新 更多