0

I am in the middle of a tutorial project of building somewhat a tourist guide. I am to use a travel advisor api to get data on all restaurants, hotels and attractions from anywhere we go to on our map. in relation, i am also using google maps api to render out a map and change the longitude and latitude and get different restaurants and hotels as the user keeps moving around the map. the problem is that i need to specify the bounds of the map, which is what is used to dynamically get the longitude and latitude from the map. i create a state for the bounds, initialize it to null, pass it down as props to get the longitude and latitude from the user's map, and then send them back as parameters to the function communicating with the api, so that the api can get the specific restaurants, hotels and attractions based off those longitude and latitude parameters, the problem is that the bounds keep coming back as null. Ive looked for hours and i cant find a single error. How do i fix it?

This is my code:

Index.js:

 import axios from 'axios'

 const URL = 'https://travel-advisor.p.rapidapi.com/restaurants/list-in-boundary'

 export const getPlacesData = async (sw, ne) => {
     try{
         const {data:{data}} = await axios.get(URL, 
                 {params: {
                     bl_latitude: sw.lat,
                     tr_latitude: ne.lat,
                     bl_longitude: sw.lng,
                     tr_longitude: ne.lng,
                 },
                 headers: {
                     'X-RapidAPI-Key': 'a-string-of-random-numbers',
                     'X-RapidAPI-Host': 'travel-advisor.p.rapidapi.com'
                 }}
             )
         return data
     }catch(error){
         console.log(error)
     }
 }

App.js:

 import {React, useState, useEffect} from 'react'

 import {CssBaseline, Grid} from '@material-ui/core'

 import Header from './components/Header/Header'
 import List from './components/List/List'
 import Map from './components/Map/Map'
 import { getPlacesData } from './api'

 function App(){ 
     const [places, setPlaces] =  useState([])
     const [coordinates, setCoordinates] = useState({})
     const [bounds, setBounds] = useState(null)

     useEffect(()=>{
         navigator.geolocation.getCurrentPosition(({coords: {latitude, longitude}})=>{
             setCoordinates({lat: latitude, lng: longitude})
         })
     },[])

     useEffect(()=>{
         getPlacesData(bounds.sw, bounds.ne)
             .then((data)=>{
                 console.log(data)
                 setPlaces(data)
             })
    
     },[bounds])

     return(
         <>
             <CssBaseline/>
             <Header/>
             <Grid container spacing = {3} style ={{ width: '100%'}}>
                 <Grid item xs ={12} md={4}>
                     <List/>
                 </Grid>
                 <Grid item xs ={12} md={8}>
                     <Map 
                         coordinates={coordinates}
                         setCoordinates={setCoordinates}
                         setBounds={setBounds}
                     />
                 </Grid>
             </Grid>
         </>
     )
 }
 export default App

Map.js:

 import React from 'react';
 import GoogleMapReact from 'google-map-react';
 import { Paper, Typography, useMediaQuery } from '@material-ui/core';
 import LocationOnOutlinedIcon from '@material-ui/icons/LocationOnOutlined';
 import Rating from '@material-ui/lab/Rating';

 import useStyles from './style'

 const Map = ({coordinates, setCoordinates, setBounds}) =>{
     const classes = useStyles()
     const isMobile = useMediaQuery('(min-width: 600pxx)')

     return(
         <div className = {classes.mapContainer}>
             <GoogleMapReact
                 bootstrapURLKeys={{key: 'string-of-some-random-letters'}}
                 defaultCenter = {coordinates}
                 center={coordinates}
                 defaultZoom={14}
                 margin={[50, 50, 50, 50]}
                 options={''}
                 onChange={(e) => {
                     setCoordinates({lat: e.center.lat, lng: e.center.lng})
                     setBounds({ne: e.marginBounds.ne, sw: e.marginBounds.sw})
                 }}
                 onChildClick={''}
             >

             </GoogleMapReact>
         </div>
     )
 }
 export default Map

This is the exact error it keeps bringing out:
Uncaught TypeError: Cannot read properties of null (reading 'sw')

PS. i know my explanation might seem confusing to anyone that hasnt worked with a maps api but i would really appreciate it if i get a response.

gerard
  • 221
  • 1
  • 2
  • 10
  • in your 'getplacesData' function, what's returned when you type if(data) console.log(data) – elguapo Aug 30 '22 at 21:20
  • thank you for replying. im not sure i understand what you mean though. assuming you meant type it inside the .then promise then it doesnt make a difference. – gerard Aug 30 '22 at 22:20
  • I believe your problem is that you're using bound.sw as a parameter in your getPlaces() useEffect hook. Your bound is set to null as a state at first, and the useEffect is running while bound is still null. – elguapo Aug 31 '22 at 21:02

0 Answers0