0

I need to display all events of user from his calendars. I get a list of all calendars and then loop through each one get events and try to store them in an array.

app.get('/getEventsList/', function (req, res) {
newArray = [];
function Done(){
console.log(newArray);
}

function getEventsforOneCalendar(token,calid){
gcal(token).events.list(calid, function(err, eventsList) {
            newArray.push(eventsList);
             });
}
function getEventsList(token) {

    gcal(token).calendarList.list(function (err, calendarList) {
        if (err) {
     //handle error
        } else {
            calendars = calendarList.items;
            forEach(calendars, function (item, index) {
           getEventsforOneCalendar(token,item.id); 
        }, Done);

        }
    });
}
getEventsList('xxxxxxxxxxxtoken');

});

Problem is: that line newArray.push(eventsList);

Any value even static passed in this line doesn't go like newArray.push('test'); and no error is thrown. if I log it I see it in the console, but it's never added to the array.

What's possibly wrong?

Ben Scarberry
  • 943
  • 2
  • 8
  • 11
  • It depends on where your `newArray` scope belongs to and when are you calling it. Since `getEventsforOneCalendar` call is async it's output may not be available at the point of time you are looking for it. Please put more code in here so that we can find out where are you exactly using `newArray` to show the eventlist. – Kamrul Sep 30 '13 at 02:14
  • Kamrul- I added the code, it's just one function when the user request the page (getEventsList). I definitely think it's a scope problem but unsure how to solve it. Thanks! – Ben Scarberry Sep 30 '13 at 02:30
  • if newArray is local, add `var` in front of the second line. – Traveling Tech Guy Sep 30 '13 at 02:45
  • @TravelingTechGuy I tried that and still. If I move the 'console.log' before the 'gcal(token).events.list(calid, function(err, eventsList)' with a static content as 'console.log('test')' it access the array and stores info succesfully. Same line after 'gcal(token).events.list(calid, function(err, eventsList)'doesn't work – Ben Scarberry Sep 30 '13 at 02:52
  • it's definitely a scope issue. My solution would be to pass newArray as a method argument to getEventsList which in turn passes it to getEventsforOneCalendar. This approach also makes your unit tests easier to write since you don't need to worry about global variables. – pinoyyid Sep 30 '13 at 03:03

1 Answers1

1

I simplest way can be like this. All it depends on how you want to show it.

app.get('/getEventsList/', function (req, res) {
var newArray = [];
var total;
function display() {
   console.log(newArray);
}

function getEventsforOneCalendar(token,calid){
gcal(token).events.list(calid, function(err, eventsList) {
                newArray.push(eventsList);
                if (total == newArray.length)
                   display();
             });
}
function getEventsList(token) {

    gcal(token).calendarList.list(function (err, calendarList) {
        if (err) {
     //handle error
        } else {
            calendars = calendarList.items;
            total = calendars.length
            forEach(calendars, function (item, index) {
                getEventsforOneCalendar(token,item.id); 
            }, Done);

        }
    });
}
getEventsList('xxxxxxxxxxxtoken');

});
Kamrul
  • 7,175
  • 3
  • 31
  • 31
  • Thank you Kamrul. but still `newArray.push(eventsList);` can't access newArray from this scope. I don't know why. When the display function is invoked, the array is empty – Ben Scarberry Sep 30 '13 at 02:47
  • i have added `var` in front of `newArray` for the scope of the `app.get` callback. This will define the `newArray`. Please check again. – Kamrul Sep 30 '13 at 03:06
  • Kamrul, you were right! THANK YOU SO MUCH! – Ben Scarberry Sep 30 '13 at 03:11