0

I am trying to get one of the events outside and print it. Of course the 'answer' variable scope is only inside.

As you can see, this one gets a list of all the events stored on a specific calendar. I am able to access specific events from the array. However. I don't know how to get that one value out of this.

I am not that familiar with coding, help appreciated.

//my question is just above the last line.

    let google = require('googleapis');
    let privatekey = mypk.json;

    // configure a JWT auth client
    let jwtClient = new google.auth.JWT(
           privatekey.client_email,
           null,
           privatekey.private_key,
           ['https://www.googleapis.com/auth/calendar']);
    //authenticate request
    jwtClient.authorize(function (err, tokens) {
     if (err) {
       console.log(err);
       return;
     } else {
       console.log("Successfully connected!");
     }
    });



    let calendar = google.calendar('v3');

    calendar.events.list({
       auth: jwtClient,
       calendarId: 'xxxxx@group.calendar.google.com'
    }



    , function (err, response, cb) {
       if (err) {
           console.log('The API returned an error: ' + err);
           return;
       }
    var events = response.items;
    var singleEvent = events[0].summary;
    return;


    /*   if (events.length == 0) {
           console.log('No events found.');
       } else {
           console.log('Event from Google Calendar:');
           for (let event of response.items) {
               console.log('Event name: %s, Creator name: %s, Create date: %s', event.summary, event.creator.displayName, event.start.date);
           }
       }*/


    }




    );


//this is what I need to get, one event but the variable has no scope here.

    console.log ('this is the ' + singleEvent);
JSnoobie
  • 3
  • 3
  • 1
    The problem is `calendar.events.list()` is asynchronous. This means it will fire off the request to google and then continue with the script. It will run `console.log()` and then sometime after that google will respond, your callback will be fired, and you'll attempt to set a variable. You need to pass the `singleEvent` to another function *inside* your callback or directly deal with the event inside the callback. See also: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Mark May 03 '18 at 16:36

1 Answers1

0

Declare the variable singleEvent just after calendar Like this:

let calendar = google.calendar('v3');
let singleEvent;

And inside the callback do the following:

singleEvent = events[0].summary;

Note: Understanding scopes in Javascript is probably the most important thing you need to learn. This article will probably help you a lot: https://scotch.io/tutorials/understanding-scope-in-javascript

Cristian S.
  • 937
  • 5
  • 13
  • I might add that understanding async operations is as important as scope. `calendar.events.list()` is asynchronous, which means that setting its return value to a global variable doesn't really help here because the `console.log()` will happen before the variable is set. – Mark May 03 '18 at 16:30