简体   繁体   中英

TypeError: undefined is not an object (evaluating 'details.geometry.location')

I'm using react-native-google-places-autocomplete package in my app. There are two fields that uses this package:- Current Location and Destination. Current Location works perfectly fine without error after i entered the location and set the value of lat/lng using details.geometry.location . However, the typeError only occurs after i set the Destination location. Below is my code.

    <GooglePlacesAutocomplete
         placeholder=""
         query={{
             key: GOOGLE_PLACES_API_KEY,
             language: 'en', // language of the results
         }}
         onPress={(data, details:any) => { setDestinationLatitude(details.geometry.location.lat.toString()); setDestinationLongitude(details.geometry.location.lng.toString()); }}
         onFail={(error) => console.error(error)}
         requestUrl={{
             url:
                'https://cors-anywhere.herokuapp.com/https://maps.googleapis.com/maps/api',
             useOnPlatform: 'web',
}} // this in only required for use on the web. See https://git.io/JflFv more for details.
             keyboardShouldPersistTaps='always'
             styles={{
                  textInputContainer: {
                    width: "90%",
                    //top: 8,
                    alignSelf: 'center'
                  },
                  textInput: {
                    borderColor: grey,
                    borderWidth: 1,
                    borderRadius: 5,
                    height: 48,
                    paddingBottom: 8,
                    color: black,
                    fontSize: 16,
                  },
                  predefinedPlacesDescription: {
                    color: '#1faadb',
                  },
             }}
    />

The following code is exactly what I use for the Current Location field. But why am I getting this typeError when they're exactly the same copy? The only difference are destinationlatitude and destinationlongitude parameters.

Oh I found the cause now. It's because I didn't set the fetchDetails to true. Silly me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM