Question

I am getting this issue when calling Shared and Recent OneDrive files using MS Graph Client on my modern webpart.

Uncaught (in promise) TypeError: Cannot read property 'id' of undefined

I get the response OK.

using /me/drive/sharedWithMe and /me/drive/recent if I just console.log it lists the ids.

It occurs when pushing the id into the items array.

it works fine with /me/drive/root/children and if I send a drive-id & item id for example /drives/{drive-id}/items/{item_id}/children

if (response) {
    console.log(response)
    response.value.map((item: any) => {
       console.log(item.id)
       items.push({
          key:item.id
          ...
       });
    });
}

I've tried

var i:any;
var item:any;
for (i in response.value) {
   item=response.value[i];
   console.log(item.id)
   items.push({
        key:item.id
        ...
   });
}

Any ideas?

No correct solution

OTHER TIPS

Works fine when I debug in my local, you could try to debug your code.

enter image description here

enter image description here

Try below solutions:

var items = response.value.map(obj => ({
    key: obj.id
}))

OR

var items = [];
var len = response.value.length;
for (var i = 0; i < len; i++) {
    items.push({
        key: response.value[i].id,
    });
}

Reference:

How to create an array of object literals in a loop?.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top