ga-wdi-exercises/project4

jquery adding blank divs...

Closed this issue · 1 comments

I'm not sure why I'm occassionally getting blank container divs appending to my page. Screenshot below. The container div is being appending inside the if statement checking if the place exists and isn't undefined.

I did try moving the line setting the container variable below the start of the if statement, but I'm still getting it.

It's hard to recreate as it doesn't happen every time. I used to see it on an error off of the reviews if statement, screenshot of that too, but I fixed that and thought it would solve the issue.

Lines 138-171 here: https://github.com/colleeno/weather-app/blob/master/public/script.js

function displayPlaces (place, status) {

 var container = $('<div></div>').addClass('place')
  if (place !==  'undefined' && place) {
    var stars = Math.round(place.rating)
  $('<h3>${place.name}</h3>').appendTo(container)
  $('<p>${place.formatted_address}</p>').appendTo(container)
  if (place.website){
  $('<p>website: <a href="${place.website}">${place.website}</a></p>').appendTo(container)
    }
  if (stars === 5){
  $('<p> &#9733; &#9733; &#9733; &#9733; &#9733;</p>').appendTo(container)
    }
  else if (stars === 4) {
  $('<p> &#9733; &#9733; &#9733; &#9733; &#9734;</p>').appendTo(container)
    }
  else if (stars === 3) {
    $('<p> &#9733; &#9733; &#9733; &#9734; &#9734;</p>').appendTo(container)
  }
  else if (stars === 2) {
    $('<p> &#9733; &#9733; &#9734; &#9734; &#9734;</p>').appendTo(container)
  }
  else if (stars === 1){
  $('<p> &#9733; &#9734; &#9734; &#9734; &#9734;</p>').appendTo(container)
    }
  else {
  $('<p>&#9733; &#9733; &#9733; &#9733; &#9733;</p>').appendTo(container)
    }
  if (place.reviews !==  'undefined' && place.reviews[0] && place.reviews[0].text.length > 0) {
  $('<p class="review">"${place.reviews[0].text}"</p>').appendTo(container)
    }
 container.appendTo('#placeResults')
}
scrollPlace() }

no error message, but empty divs
empty divs

used to have this error, but fixed it
fixed this error message

*edit - I do still sometimes get the 'Uncaught TypeError: Cannot read property '0' of undefined' at 165, but it doesn't seem to correlate with the empty divs