我正在尝试将其中一个事件发送到外面并打印出来。当然,“回答”变量范围只在内部。
如您所见,这个获取存储在特定日历上的所有事件的列表。我能够从阵列中访问特定事件。然而。我不知道如何从中得到这一个值。
我对编码并不熟悉,非常感谢。
//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);
答案 0 :(得分:0)
在singleEvent
之后声明变量calendar
像这样:
let calendar = google.calendar('v3');
let singleEvent;
在回调中执行以下操作:
singleEvent = events[0].summary;
注意:了解Javascript中的范围可能是您需要学习的最重要的事情。本文可能会对您有所帮助: https://scotch.io/tutorials/understanding-scope-in-javascript