2017-12-10 8 views
1

を読み取れませんGoogleカレンダーから選択したデータを抽出し、クライアント側でng-repeatを使用してフォームに表示しようとしています。サーバ側では、私が原因JSONGoogleカレンダーのネスト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'}] 
}] 
}; 

第一層は項目から、オブジェクトの内部アイテムに取得することです

ネストされた簡素化されたカレンダーの理解の欠如にデータをループにできませんよ概要, start(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 

まず、私はそこから私はループのために書いた、どこで、これがある項目から項目

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

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

にアクセスすることができ、オブジェクトを配列に変換がありますそれはうまくいかなかった。 calendararray.items [0]要約を含むリストがある、起動エラーメッセージがあるは未定義

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てきたのプロパティ「長さ」を読み込めません参加者

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' ] 

してください任意のヘルプやアドバイス

+0

を(VAR I = 0; I cdoshi

+0

calendarListはオブジェクトなので、@ cdoshiは動作しません。その長さは定義されていません。 – miintea

答えて

1

まず私はそこから私はあなたが間違っていたところだ項目

にアクセスすることができ、オブジェクトを配列に変換します。何かに何かを変換しないでください。データをそのままそのまま使用することができます。

.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); 
 
    }); 
 
});

+0

ありがとう@Michael Geary!これは非常に便利です!私はそれを働かせることができます。今、課題はMongoDBに多くのアイテムを保存することです – miintea

関連する問題