Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length')

3.1k Views Asked by At

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>
0

There are 0 best solutions below