繁体   English   中英

无法获取未定义或null引用javascript的属性

[英]Unable to get property of undefined or null reference javascript

当我从初始化函数启动一个函数时,我收到消息

“无法获取未定义或空引用的属性'address_components'”

我首先添加一个监听器

google.maps.event.addDomListener(window, 'load', initialize);

这是我的初始化功能

function initialize() {
   document.getElementById('autocomplete').value =   city()+','+countryName();  
   autocomplete = new google.maps.places.Autocomplete(
    /** @type {HTMLInputElement} */(document.getElementById('autocomplete')),
                                   {types: ['establishment','geocode']});

    fillInAddress();  --> this call does not work

    google.maps.event.addListener(autocomplete, 'place_changed', function() {
                    fillInAddress();  --> this call work when I change manually the value                   in                      the     field
                });
}

以下调用的函数在由上述侦听器触发时起作用(place_changed)

function fillinAddress()

     var place = autocomplete.getPlace();
     console.log(place);    --> this gives undefined  when call from initialize function

     for (var i = 0; i < place.address_components.length; i++) {  ..  --> this gives error 

     //Unable to get property 'address_components' of undefined or null reference

有什么帮助吗?

您需要将自动完成功能传递给类似

function initialize() {fillinAddress(autocomplete)}
function fillinAddress(autocomplete){console.log(autocomplete);}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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