M
M
Matvey Kremnin2016-07-27 13:11:59
JavaScript
Matvey Kremnin, 2016-07-27 13:11:59

Why is an error thrown ( Uncaught TypeError: Cannot read property '0' of undefined ) and how to fix it???

Help me solve the problem, I get the error Uncaught TypeError: Cannot read property '0' of undefined
I'm working with the weather API, the error is in the same line

' Weather: ' + res.weather[0].main + ''+


How can I overcome it with if else ?
Thanks for the tip

function openModal(res) {
            if(Object.getOwnPropertyNames(res).length === 0) {
                contentString = 'To much long, come closer.';
            } else {
                contentString = '<div id="content">'+
                        '<div id="siteNotice">'+
                        '</div>'+
                        '<div><b>Place: </b><span id="place"> ' + res.name + '</span> </div>'+  
                        '<p><b>Weather:</b> <span id="weather">' + res.weather[0].main + '</span></p>'+
                        '<p><b>Temperature:</b> <span id="temperature">' + (res.main.temp - 273.15).toFixed(offset = 0) +'</span>&#176 C</p>'+
                        '<p><b>Wind speed:</b> <span id="wind">' + res.wind.speed + '</span> ms</p>'+ 
                        '<p><b>Humidity:</b> <span id="humidity">' + res.main.humidity + '</span>%</p>'+ 
                        '</div>';
            }

            infowindow = new google.maps.InfoWindow({
                content: contentString
            });

            infowindow.open(map, marker);
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nikosias, 2016-07-27
@nikosias

'<p><b>Weather:</b> <span id="weather">' + ((res.weather && res.weather[0] && res.weather[0].main)?res.weather[0].main:'') + '</span></p>'+

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question