0

I need to get my details using API on Webex, but i when i click on the button that suppose to get an output of my details, instead i get this error.

edit: I tried to add console.log(data) as commenter huy suggested, but it only displays my details on the console, and it still has the error length data.items undefined. How do I define the data.items? Anyway, thank you commenters for answering my question.

    document.getElementById('getDetail').addEventListener('click', getDetail);

    function getDetail(){
        fetch('https://webexapis.com/v1/people/me', {
            method: 'GET',
            headers: {
                'Content-type': 'application/json',
                'Authorization': `Bearer ${token}`
            },
        })
        .then(function(res){
            return res.json();
        })
        .then(function(data){
            let output ='';
            for(let i=0;i<data.items.length;i++){
                output+=`
                     <ul>
                        <li>${data.items[i].id}</li>
                        <li>${data.items[i].displayName}</li>
                        <li>${data.items[i].nickName}</li>
                        <li>${data.items[i].firstName}</li>
                        <li>${data.items[i].lastName}</li>
                        <li>${data.items[i].emails}</li>
                    </ul>
                `;
            }
            document.getElementById('getDetail').innerHTML = output;
        })
    }
</script>
anonymous
  • 1
  • 1
  • 3
  • 2
    Your data.items is undefined, so you can't call data.items.length. Please add console.log(data) ... – Huy Nguyen Nov 13 '21 at 05:41
  • @huy is correct. data.items is undefined... – Lakshaya U. Nov 13 '21 at 06:09
  • @huy i tried to add console.log(data) but its only display the details on console, and the error undefined is still there. How do i define the data.items? btw thankyou for answering my question! :) – anonymous Nov 13 '21 at 06:20
  • That must be defined by the response that is sent by the back-end, i.e. the server. We have no control over that. However, you can still do it like so: `data.items = (...)`. Also, it is better not to share your access tokens or api keys on SO. – Lakshaya U. Nov 13 '21 at 06:21
  • @hacKaTun3s I see... alright! thankyou so much! – anonymous Nov 13 '21 at 06:34
  • You can have a null check for the items before using it in the for loop `var items = data && data.items ? data.items: []; for(let i=0 ;i – Shreyas Kumar Nov 13 '21 at 06:42
  • @ShreyasKumar does have the solution to it. However, that will make no sense. Why would you waste time defining a property which doesn't exist instead of doing what you wanna do with the data itself? Because eventually you will have to use something from the original response data itself to carve out the `items` in it, right? – Lakshaya U. Nov 13 '21 at 07:28
  • @hacKaTun3s exactly! we don't need to do it if the response is proper. – Shreyas Kumar Nov 13 '21 at 07:48

0 Answers0