0

I am trying to build a weather app however, i am not sure why when entering a location to find the weather of a specific area, is only returning the longitude and latitude coordinates in my terminal on VS code instead of returning it on my console in chrome like I am telling it to. It should be returning my API call that I am making, returning a whole bunch of data that my API is showing. Here is my code:

const searchElement = document.querySelector('[data-location-search]')
const searchBox = new google.maps.places.SearchBox(searchElement)
searchBox.addListener('places_changed', () => {
const place = searchBox.getPlaces()[0]
    if (place == null) return
    const latitude = place.geometry.location.lat()
    const longitude = place.geometry.location.lng()
    fetch('/weather', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        },
        body: JSON.stringify({
            lat: latitude,
            lng: longitude
        })
    }).then(res => res.json()).then(data => {
        console.log(data)
    
        // setWeatherData(data, place.formatted_address)
    })
})
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • Is this clientside JS (tag `JavaScript`) or serverside JS (tag `node.js`) - please tag accordingly. – Daniel W. Aug 25 '20 at 16:11
  • Does this answer your question? [Output to Chrome console from Node.js](https://stackoverflow.com/questions/11704292/output-to-chrome-console-from-node-js) – Daniel W. Aug 25 '20 at 16:12
  • this is Javascript. My apologies – Keaton Proctor Aug 25 '20 at 16:14
  • How does `console.log()` return data to your console in VS Code when this is clientside JavaScript which you run from within the browser? It's not possible imo. – Daniel W. Aug 25 '20 at 16:42
  • Probably the log in your terminal is not the exact one in your sample code. Are you sure `const place = searchBox.getPlaces()[0]` is not null or undefined? Or maybe it would be helpful if you add a `.catch` block of code after your `fetch` request and see if it's failing. – Jojo Narte Aug 25 '20 at 16:48
  • Could you provide me with what I should put for a .catch block and how it would be typed out? – Keaton Proctor Aug 25 '20 at 17:46

0 Answers0