"ReferenceError: calendar is not defined" encountered in NodeJS but same code works in API Test Console in Google

"ReferenceError: calendar is not defined" encountered in NodeJS but same code works in API Test Console in Google

正在尝试关注此博客 post Create a Smart Voicemail with Twilio, JavaScript and Google Calendar

当我 运行 Google 开发人员 API 测试控制台中的代码时,它可以工作。但是,在 运行s NodeJS returns 的 Twilio 函数中调用相同的参数会出现错误“ReferenceError: calendar is not defined”

我制作了 Google 日历事件 public,我尝试使用 public URL 查看它,它也有效。由于某些原因,使用 Twilio Functions 调用它会导致错误。

const moment = require('moment');
const {  google } = require('googleapis');

exports.handler = function(context, event, callback) {

  // Initialize Google Calendar API
  const cal = google.calendar({
    version: 'v3',
    auth: context.GOOGLE_API_KEY
  });

  //Read Appointment Date
  let apptDate = event.ValidateFieldAnswer;

  var status = false;

  const res = {
    timeMin: moment().toISOString(),
    timeMax: moment().add(10, 'minutes').toISOString(),
    items: [{
      id: context.GOOGLE_CALENDAR_ID
    }]
  };
  console.log(res);

  cal.freebusy.query({
    resource: res
  }).then((result) => {
    const busy = result.data.calendars[calendar].busy;
    console.log("Busy: " + busy);
    if (busy.length !== 0) {
      let respObj1 = {
        "valid": false
      };
      console.log("Failed");
      callback(null, respObj1);
    } else {
      let respObj1 = {
        "valid": true
      };
      console.log("Success");
      callback(null, respObj1);
    }
  }).catch(err => {
    console.log('Error: checkBusy ' + err);
    let respObj1 = {
      "valid": false
    };
    callback(null, respObj1);
  });

};

您以前遇到过这个问题吗?或者有人能找出这个问题吗?

谢谢

这一行似乎是问题所在:

const busy = result.data.calendars[calendar].busy;

据我所知,calendar 从未定义过。这应该可以代替:

const busy = result.data.calendars[context.GOOGLE_CALENDAR_ID].busy;

看起来这行代码在教程的 "Google Calendar FreeBusy Queries" 和 "Recording VoiceMails" 部分有所不同,需要在后面的代码示例中更新。