无法读取属性undefined

问题描述:

什么是我的承诺有这个错误的承诺?无法读取属性undefined

无法读取属性 '然后' 未定义

it('EmitAreaChanged_AreaEntered_AreaPlaceValueMakesItToHost', async(() => { 
    let htmlInput = document.createElement("INPUT"); 
    htmlInput.setAttribute("type", "text"); 
    let autocompleteMock = new GoogleAutocompleteMock(testHost.areaPicker, htmlInput); 
    let x = autocompleteMock.autocomplete.set("place", autocompleteMock.place); 
    x.then(() => { 
     expect(testHost.placeValue).toBe(true); 
    }); 
    return x; 
})); 

GoogleAutocompleteMock的(与其说是模拟 - 更多的是真正的自动完成的):

export class GoogleAutocompleteMock { 
    autocomplete: any; 
    area: google.maps.LatLngBounds = new google.maps.LatLngBounds(
     new google.maps.LatLng(-47.746617, 165.346753), 
     new google.maps.LatLng(-33.670946, -179.667808) 
    ); 
    options: Object = { 
     bounds: this.area 
    }; 

    constructor(component: any, htmlInput: any) { 

     this.autocomplete = new google.maps.places.Autocomplete(
     <HTMLInputElement>htmlInput, this.options); 
     this.autocomplete.addListener('place_changed',() => { 
     component.getAddress(this.autocomplete) 
     }); 
    } 

    place = { "address_components": [{ "long_name": "Auckland", "short_name": "Auckland", "types": ["locality", "political"] }, { "long_name": "Auckland", "short_name": "Auckland", "types": ["administrative_area_level_2", "political"] }, { "long_name": "Auckland", "short_name": "Auckland", "types": ["administrative_area_level_1", "political"] }, { "long_name": "New Zealand", "short_name": "NZ", "types": ["country", "political"] }], "adr_address": "<span class=\"locality\">Auckland</span>, <span class=\"country-name\">New Zealand</span>", "formatted_address": "Auckland, New Zealand", "geometry": { "location": { "lat": -36.8484597, "lng": 174.76333150000005 }, "viewport": { "south": -37.0654751, "west": 174.44380160000003, "north": -36.660571, "east": 175.2871371 } }, "icon": "https://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png", "id": "088418ddc17fef2513462d92dbee1355929b35ed", "name": "Auckland", "photos": [{ "height": 1836, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/111644522305605838507/photos\">Johannes De Smedt</a>"], "width": 3264 }, { "height": 2988, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/104449230804367883642/photos\">Michal Panek</a>"], "width": 5312 }, { "height": 3096, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/115717451821958551526/photos\">Sakchhyam Malla</a>"], "width": 4128 }, { "height": 3672, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/100726048745034015308/photos\">Maria Bitunjac</a>"], "width": 4896 }, { "height": 1836, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/112349734260069492163/photos\">Carol Prichard</a>"], "width": 3264 }, { "height": 3120, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/106356366323887392782/photos\">Kovács György</a>"], "width": 4208 }, { "height": 1504, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/117137027694384717244/photos\">Martin Mobers</a>"], "width": 2006 }, { "height": 492, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/102033645464260143092/photos\">Priyesh Bhavsar</a>"], "width": 1000 }, { "height": 2988, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/103221298159348509467/photos\">Alvie Granito</a>"], "width": 5312 }, { "height": 3265, "html_attributions": ["<a href=\"https://maps.google.com/maps/contrib/100418135834547049744/photos\">Simon Chen</a>"], "width": 4898 }], "place_id": "ChIJ--acWvtHDW0RF5miQ2HvAAU", "reference": "CmRbAAAAEJGwhQ0l3O1QapXvbJ_s-xNVyh7UxBjd89D9Q860dfhb5Xh3xeY95UT1tIdugiTRhhZHT2qk_wzUqfd3wPS-vdX1pBljxcPtMUqhTtslzMyVccViA9ckk50Xv_cFWNXFEhCPkvW84Okrk2SJUpUDLGz8GhSEN1dm_0tJj4nEHYo0-bkRdOykYQ", "scope": "GOOGLE", "types": ["locality", "political"], "url": "https://maps.google.com/?q=Auckland,+New+Zealand&ftid=0x6d0d47fb5a9ce6fb:0x500ef6143a29917", "utc_offset": 780, "vicinity": "Auckland" } 
} 
+2

似乎'set'方法没有返回承诺。你能在这里包括'GoogleAutocompleteMock'的代码吗? – 31piy

+0

@ 31piy您的评论颇有启发,我不认为google.maps.places.autocomplete.set会返回承诺。谢谢 – BeniaminoBaggins

无法读取属性“,然后'未定义

是因为x未定义。

当您访问x.then你真的这样做......

undefined.then 

这显然是一个错误,因为不确定是不是一个承诺和undefined没有属性。

确保x被定义,并且是在你调用之前的承诺。