【问题标题】:Access "busy" array in google calendar api在谷歌日历 API 中访问“忙碌”数组
【发布时间】:2021-06-17 14:32:25
【问题描述】:

尝试在 Google Calendar API 中使用 freebusy 查询。我按照Googleapis documentation 提供的示例进行操作,确实得到了结果,但忙碌信息仅显示为[Array],没有实际的忙碌信息。

代码(只是相关部分):

const res = await calendar.freebusy.query({
        requestBody: {
            timeMin: (new Date()).toISOString(),
            timeMax: '2021-03-25T23:00:00+01:00',
            items: [
                {
                   id: 'calendarid'
                }
            ]
        },
    });

    console.log(res.data);

得到正确的响应(意味着没有错误),但响应总是将“busy”属性显示为 [Array]:

{
  kind: 'calendar#freeBusy',
  timeMin: '2021-03-20T10:39:42.000Z',
  timeMax: '2021-03-25T22:00:00.000Z',
  calendars: {
    'calendarid': { busy: [Array] }
  }
}

正确的完整结果(使用Google API Explorer 测试):

{
 "kind": "calendar#freeBusy",
 "timeMin": "2021-03-20T11:00:00.000Z",
 "timeMax": "2021-03-25T22:00:00.000Z",
 "calendars": {
  "calendarid": {
   "busy": [
    {
     "start": "2021-03-22T18:00:00Z",
     "end": "2021-03-22T19:00:00Z"
    },
    {
     "start": "2021-03-24T13:00:00Z",
     "end": "2021-03-24T16:00:00Z"
    }
   ]
  }
 }
}

知道如何访问“忙碌”数组中的信息...?

【问题讨论】:

    标签: node.js arrays json google-api google-api-nodejs-client


    【解决方案1】:

    通常当您在控制台记录嵌套对象(或您的情况下的响应主体)时,您会看到嵌套对象值为[object object][Array]。您仍然可以解析对象以获取嵌套值,但如果您想在控制台日志中查看完整响应,可以尝试 console.log(JSON.stringify(res.data))

    解析响应并获取busy数组。

    const busyArray = res.data.calendars.calendarid.busy;
    

    【讨论】:

    • 谢谢,JSON.stringify 发现了它的工作原理。但此外,“日历”的格式为“somethingsomething@group.calendar.google.com”。所以我不能打电话给“res.data.calendars.somethingsomething@group.calendar.google.com.busy”。如何访问此数组的 { "start", "end" } 个对象?
    • 在这种情况下,您可以使用res.data.calendars[calendarIdVariable].busy 之类的索引表示法访问该属性。但在使用它之前,您必须将日历 ID 存储在 calendarIdVariable 变量中。
    • 太棒了。非常感谢 - 我知道这很简单,所以感谢学校教育。将此标记为已解决。
    • 很高兴能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    相关资源
    最近更新 更多