【问题标题】:Cannot read property 'then' of undefined无法读取未定义的属性“then”
【发布时间】:2017-04-21 22:02:34
【问题描述】:

什么是我的承诺有这个错误?

无法读取未定义的属性“then”

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" }
}

【问题讨论】:

  • 似乎set 方法没有返回承诺。可以把GoogleAutocompleteMock的代码放在这里吗?
  • @31piy 您的评论很有启发性,我认为 google.maps.places.autocomplete.set 不会返回承诺。谢谢

标签: angular typescript jasmine promise


【解决方案1】:

无法读取未定义的属性“then”

是因为 x 未定义。

当你访问 x.then 时,你真的在​​做......

undefined.then

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

确保 x 已定义并且是一个承诺,然后再调用它。

【讨论】:

    猜你喜欢
    • 2019-08-19
    • 2016-10-23
    • 2014-09-07
    • 2015-09-08
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多