1

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;}
            } 
            }
            });
    });
};
  • How does your shifts JSON look? – Sharjeel Ahmed Feb 17 '17 at 14:18
  • @SharjeelAhmed An entire user object is as follows { "_id" : ObjectId("589f60999c471a32c6a3a380"), "name" : "Ciara", "email" : "ciara@test.com", "company" : "Apple ", "username" : "Ciara", "password" : "$2a$10$QkaRzb7W/BOzvtnY8hPtQ.j3GWy.XI92X6osh3m4/TNEuPBrAprva", "position" : "Sales assistant", "location" : "Dublin", "admin" : 0, "shifts" : [ "day", "day", "night", null, null, null, null, "23" ] } – LiveLongCandy51 Feb 17 '17 at 14:35
  • Looks fine to me, where are you facing a problem? – Sharjeel Ahmed Feb 18 '17 at 20:13
  • @SharjeelAhmed The above code has no effect, if I remove the "if(i==0){" statement then the code iterates through a user and checks if they have two 'days' in total in their shifts array whereas I want to iterate through all the users and check if there are two of them with 'day' in just their shifts[0] – LiveLongCandy51 Feb 19 '17 at 03:53

1 Answers1

0

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.

Sharjeel Ahmed
  • 2,051
  • 2
  • 17
  • 25
  • That looks right to me as well however it does not work for me, irrelevant of how many days the accounts choose the buttons are never disabled – LiveLongCandy51 Feb 20 '17 at 13:21
  • paste your html here also any error on your console? – Sharjeel Ahmed Feb 21 '17 at 12:30
  • The only places i refer to the script in the html are – LiveLongCandy51 Feb 22 '17 at 13:11
  • A sample of the shifts table is as follows:

    Monday

    – LiveLongCandy51 Feb 22 '17 at 13:12
  • hardcode the "data" variable as i have given above, and let me know if it works, else u need to paste the entire JSON what you get from the ajax call – Sharjeel Ahmed Feb 22 '17 at 17:52
  • _id: 589b4af0714d451fa002dec0, name: 'SteveTest', email: 'steve@test.com', company: 'RegTest2', username: 'SteveTest', password: '$2a$10$yG3tczaBOrirY8Y1hXJ8vujrvPI0QdI7wc./WxWKV.9v5PEb128l2', position: 'RegTest2', location: 'RegTest2', admin: 0, __v: 0, shifts: [ 'day', 'day', null, 'day', 'night', null, null, '31' ], score: [ 400, 940, 120, 50, 0 ] } – LiveLongCandy51 Feb 23 '17 at 10:25
  • _id: 589f4fd39c471a32c6a3a37f, name: 'Mark', email: 'mark@eurogiant.com', company: 'Giant', username: 'Mark', password: '$2a$10$UJNMbhPoHVLPizueOkH2Se1SNXCOAnM8zc8F9nXSul7kCPYF5OdLC', position: 'Sales assistant', location: 'Dublin', admin: 0, __v: 0, shifts: [ 'day', 'day', 'day', null, 'day', null, null, '32' ], score: [ 360, 950, 370, 350, 0 ] } There are several other accounts but thats what two entire ones look like, thanks again for your continued help @SharjeelAhmed – LiveLongCandy51 Feb 23 '17 at 10:27
  • Hey, yeah your example works perfectly for me in jsfiddle however it wont work in my actual code, its really quite frustrating. Thanks again for all the help – LiveLongCandy51 Feb 24 '17 at 15:11
  • can do put console.log(JSON.stringify(data)) after $.each(data, function(val) { and let me know what you get. Want to give this a final try – Sharjeel Ahmed Feb 27 '17 at 10:37
  • Thanks for the continued support, when I add that line nothing prints out. The shift.js file is accessed but nothing prints in the console log relating to the data – LiveLongCandy51 Mar 01 '17 at 13:25
  • var studentListData = []; var shiftArray = new Array(); shiftArray[0]=8; shiftArray[1]=7; $(document).ready(function() { populateTable(); $('#studentList table tbody').on('click', 'td a.linkshowstudent', showStudentInfo); var data = db.getCollection("users") var dayCount=0; var nightCount=0; $.each(data, function(val) { console.log(JSON.stringify(data)) studentListData = data; for (i = 0; i < 7; i++) { if (i==0 && this.shifts[i] == 'day') { dayCount++; – LiveLongCandy51 Mar 01 '17 at 13:41
  • 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;}}}});}); – LiveLongCandy51 Mar 01 '17 at 13:41
  • Thats what my current shifts.js file looks like and then there are a few extra functions at the bottom of the file outside of the scope of $(document).ready(function() {} – LiveLongCandy51 Mar 01 '17 at 13:42
  • That means "data" is empty and you are not receiving any data from your ajax – Sharjeel Ahmed Mar 01 '17 at 13:43
  • That means "data" is empty and you are not receiving any data from your ajax. Infact you should put console.log(data) BEFORE the $.each, not after, try that – Sharjeel Ahmed Mar 01 '17 at 13:52
  • Still nothing prints, should I be making a connection to the mongodb database in shifts.js because var data = db.getCollection("users") is not getting any data? – LiveLongCandy51 Mar 01 '17 at 15:18
  • In my routes file the table is already connected to my shifts table : router.get('/shiftsTable', function(req, res) { var db = req.db; db.collection('users').find().toArray(function (err, items) { res.json(items); }); }); – LiveLongCandy51 Mar 01 '17 at 15:20
  • do a console.log(items) before res.json, if it is empty mostly you have to do a mongo connect, but I suggest you use mongoose npm. – Sharjeel Ahmed Mar 01 '17 at 17:53
  • So items is an array of objects which i believe is correct-- [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] --- also i am using mongoose but its most likely poorly integrated – LiveLongCandy51 Mar 02 '17 at 09:57
  • You need to convert the mongoose object to JSON http://stackoverflow.com/questions/9952649/convert-mongoose-docs-to-json – Sharjeel Ahmed Mar 02 '17 at 10:00
  • To update you, i have been trying to stringify the repsonse but with no luck, db.collection('users').find().lean().exec(function (err, items) { return res.end(JSON.stringify(items)); }); – LiveLongCandy51 Mar 06 '17 at 17:33
  • so is it empty? Also i see that you are using mongoDB driver not mongoose, not sure if that has the lean() method, you may have to use toArray() – Sharjeel Ahmed Mar 07 '17 at 08:55