I have created a server-rendered react
app. In different pages, I try to get multiple addresses from the user and I want to mark entered locations on Google Maps
.
For testing purposes(to get familiar with geocode
), I added react-geocode
exactly like the reference. However, I get the error:
ReferenceError: fetch is not defined at /home/.../node_modules/react-geocode/lib/index.js:1:255
This is the component:
import GoogleMapReact from 'google-map-react';
import Geocode from "react-geocode";
...//other imports
const AnyReactComponent = ({ text }) => <div>{text}</div>;
class History extends Component {
static defaultProps = {
center: {
lat: 59.95,
lng: 30.33
},
zoom: 11
};
static async getInitialProps(props) {
let addresscounts = await tracker.methods.zipCounts().call()
//////////////////////////react-geocode////////////////////
Geocode.setApiKey("~~~ API KEY ~~~");
Geocode.fromAddress("Eiffel Tower").then(
response => {
const { lat, lng } = response.results[0].geometry.location;
console.log(lat, lng);
}, error => { console.error(error) }
);
///////////////////////////////////
return {
addresscounts
};
}
render(){
return(
<div style={{ height: '100vh', width: '100%' }}>
<GoogleMapReact
bootstrapURLKeys={{ key: "~~~ API KEY ~~~"}}
defaultCenter={this.props.center}
defaultZoom={this.props.zoom}
>
<AnyReactComponent
lat={59.955413}
lng={30.337844}
text="My Marker" />
</GoogleMapReact>
</div>
);
}
}
export default History;
Can someone tell me what the problem is and how I can solve it? Do I need to send the request through the fetch
API(node-fetch)?