简体   繁体   中英

Nested loops for mongodb arrays in Node.js

Hey so I'm reasonably new to Node.js and Mongodb, I am making a roster creation system and I want to make it so that only two users/employees can work on a given shift. For simplicity the shifts are split into 'day' and 'night' which are in a table of radio buttons that the user can chosen. Onload of the shifts page however I want to disable those buttons which have already been chosen by two other users.

I think that the outer loop should iterate through the shifts array of the users (Monday --> Sunday), the inner loop should then iterate through each user in the mongodb database. Eventually I want to have the code working for each day of the week but for now just need Monday to function properly.

Thanks for any help and advise

function disableOnLoad() {
    console.log("disableOnLoad reached");
    var dayCount=0;           
    var nightCount=0;
    $.getJSON( '/shiftsTable', function( data ) {
        $.each(data, function(){
            userListData = data;         
        for(i = 0; i < 7; i++) { 
            **//$.each(data, function(){**
            if(this.shifts[i]=='day'){
             if(i==0){
                dayCount++;
                console.log("dayCount: "+dayCount);  
                if(dayCount>2){document.getElementById("monDay").disabled=true;}
             }
            }
            else if(this.shifts[0]=='night'){
                nightCount++;
                if (nightCount>2){document.getElementById("monNight").disabled=true;}
            } 
            }
            });
    });
};

Assuming this is the data

    data = [{
    "_id": "589f60999c471a32c6a3a380",
    "name": "Ciara",

    "position": "Sales assistant",
    "location": "Dublin",
    "admin": 0,
    "shifts": ["day", "day", "night", null, null, null, null, "23"]
  },
  {
    "_id": "589f60999c471a32c6a3a380",
    "name": "Ciara2",

    "position": "Sales assistant",
    "location": "Dublin",
    "admin": 0,
    "shifts": ["day", "day", "night", null, null, null, null, "23"]
  },{
    "_id": "589f60999c471a32c6a3a380",
    "name": "Ciara3",
    "position": "Sales assistant",
    "location": "Dublin",
    "admin": 0,
    "shifts": ["day", "day", "night", null, null, null, null, "23"]
  }];

So this code works for me

     $.each(data, function(val) {

    userListData = data;
    for (i = 0; i < 7; i++) { //* * //$.each(data, function(){**
      if (i==0 && this.shifts[i] == 'day') {
          dayCount++;
          if (dayCount == 2) {
            document.getElementById("monDay").disabled = true;
          }
      } else if (i==0 && this.shifts[i] == 'night') {
        nightCount++;
        if (nightCount == 2) {
          document.getElementById("monNight").disabled = true;
        }
      }
    }
  });

}

Let me know if it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM