【问题标题】:Google Calendar nested JSON -- Cannot read property 'length' of undefinedGoogle 日历嵌套 JSON - 无法读取未定义的属性“长度”
【发布时间】:2017-12-10 18:12:17
【问题描述】:

我正在尝试从 Google 日历中提取选定的数据,以便在客户端使用 ng-repeat 以表单形式显示它。在服务器端,由于缺乏对嵌套的理解,我无法遍历数据

简化的日历 JSON

const calendarList = {
items: [{
    summary: 'ABC',
    start: [{ datetTime: '2017-12-10T01:40:18.000Z'}], 
    attendees: [{ displayName: 'John'}]
},
{
    summary: 'DEF',
    start: [{ datetTime: '2017-12-11T01:40:18.000Z'}], 
    attendees: [{ displayName: 'Jane'}, { displayName: 'Mary'}]
}]
};

第一层是到对象内部的items,从items,提取summarystart(dateTime)与会者(displayName)。有可能多人参加

console.log(calendarList.items[0].summary); //ABC
console.log(calendarList.items[0].start.dateTime); //2017-12-10T01:40:18.000Z
console.log(calendarList.items[0].attendees[0].displayName); //John

首先我将对象转换为数组,从那里我可以访问 items

 var calendararray = Object.keys(calendarList)
 console.log(calendararray)

    // [ 'kind',
    // 'etag',
    // 'summary',
    // 'updated',
    // 'timeZone',
    // 'accessRole',
    // 'defaultReminders',
    // 'nextSyncToken',
    // 'items' ]

items 我写了一个 for 循环,这就是它不起作用的地方。错误消息是Cannot read property 'length' of undefined

for(var i = 0; i < calendararray.items.length; i++)
    {
        var items = calendarList.items[i];
        newGcal.summary += items.summary;
        newGcal.datetime += items.start.datetime;

        for(var j = 0; j < items.attendees.length; j++)
        {
            var attendees = items.attendees[j];
            newGcal.attendees += attendees.displayName;
        }
    }

我有 console.log calendararray.items[0] 并且有一个列表,包括 summarystartattendees

console.log(calendararray.items[0])

[ 'kind',
  'etag',
  'id',
  'status',
  'htmlLink',
  'created',
  'updated',
  'summary',
  'description',
  'location',
  'creator',
  'organizer',
  'start',
  'end',
  'iCalUID',
  'sequence',
  'attendees',
  'guestsCanInviteOthers',
  'privateCopy',
  'reminders' ]

请提供任何帮助或建议

【问题讨论】:

  • 而不是 for(var i = 0; i
  • 这不起作用@cdoshi,因为 calendarList 是一个对象,所以它会返回未定义的长度

标签: javascript json nested google-calendar-api


【解决方案1】:

首先我将对象转换为数组,从那里我可以访问 items

那是你出错的地方。不要将任何东西转换为任何东西;您可以轻松地直接处理数据。

.forEach() 对此很有帮助,因此您不必编写容易出错的for 循环。

下面是一个示例,它列出了简化的calendarList 中的所有数据。您应该可以从此处获取实际数据中的其他相关项。

我还稍微重新格式化了您的 calendarList 数据,以使对象/数组关系更加清晰:

const calendarList = {
    items: [
        {
            summary: 'ABC',
            start: [
                { datetTime: '2017-12-10T01:40:18.000Z' }
            ],
            attendees: [
                { displayName: 'John' }
            ]
        },
        {
            summary: 'DEF',
            start: [
                { datetTime: '2017-12-11T01:40:18.000Z' }
            ],
            attendees: [
                { displayName: 'Jane' },
                { displayName: 'Mary' }
            ]
        }
    ]
};

calendarList.items.forEach( function( item ) {
    console.log( 'summary:', item.summary );
    item.start.forEach( function( dt ) {
        console.log( '  start:', dt.datetTime );
    });
    item.attendees.forEach( function( attendee ) {
        console.log( '  attendee:', attendee.displayName );
    });
});

【讨论】:

  • 谢谢@Michael Geary!这非常有帮助!我设法让它工作。现在的挑战是在 MongoDB 中保存许多项目
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多