簡體   English   中英

類型錯誤:未定義不是 object(正在評估“details.geometry.location”)

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

我在我的應用程序中使用 react-native-google-places-autocomplete package。 有兩個字段使用此 package:- 當前位置和目的地。 在我輸入位置並使用details.geometry.location設置 lat/lng 的值后,當前位置工作得很好,沒有錯誤。 但是,typeError 僅在我設置目標位置后發生。 下面是我的代碼。

    <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',
                  },
             }}
    />

以下代碼正是我用於“當前位置”字段的代碼。 但是為什么當它們是完全相同的副本時我會得到這個 typeError 呢? 唯一的區別是目的地緯度和目的地經度參數。

哦,我現在找到原因了。 這是因為我沒有將fetchDetails設置為 true。 傻我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM