访问谷歌日历api中的“忙碌”数组

时间:2021-03-20 10:57:59

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

尝试在 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"
    }
   ]
  }
 }
}

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

1 个答案:

答案 0 :(得分:1)

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

解析响应并获取busy数组。

const busyArray = res.data.calendars.calendarid.busy;
相关问题