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?

5
  • 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. Commented Sep 30, 2013 at 2: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! Commented Sep 30, 2013 at 2:30
  • if newArray is local, add var in front of the second line. Commented Sep 30, 2013 at 2: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 Commented Sep 30, 2013 at 2: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. Commented Sep 30, 2013 at 3:03

1 Answer 1

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');

});
Sign up to request clarification or add additional context in comments.

3 Comments

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
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, you were right! THANK YOU SO MUCH!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.