【问题标题】:Aurelia - javascript/Typescript - Issue where typeahead calls a function before a promise is returned leading to errorAurelia - javascript/Typescript - 在返回承诺之前 typeahead 调用函数导致错误的问题
【发布时间】:2018-07-28 00:46:24
【问题描述】:

在您清除预输入字段之前,这一切都有效,然后我在函数 suburbSelected(item) 中收到错误(如下所示),因为在预输入调用此函数之前承诺尚未解决。我得到的完整错误 - 其中两个如下:

  Uncaught (in promise) TypeError: Cannot read property 'postcode' of null
    at Address.app / components / clients / address / address.Address.suburbSelected(address.ts:126)
    at CallScope.evaluate(aurelia - binding.js:1524)
    at Call.callSource(aurelia - binding.js:4961)
    at AubsTypeaheadCustomElement.<anonymous>(aurelia - binding.js:4985)
    at aubs- typeahead.js:227
    at < anonymous >
        app / components / clients / address / address.Address.suburbSelected @ address.ts: 126
    evaluate @ aurelia-binding.js:1524
    callSource @ aurelia-binding.js:4961
        (anonymous) @ aurelia-binding.js:4985
            (anonymous) @ aubs-typeahead.js:227
    Promise.then(async)
    filterChanged @ aubs-typeahead.js:222
    descriptor.set @ aurelia-binding.js:5455
    assign @ aurelia-binding.js:1398
    assign @ aurelia-binding.js:1174
    updateSource @ aurelia-binding.js:4830
        (anonymous) @ debounce-binding - behavior.js:16
    setTimeout(async)
    debounce @ debounce-binding - behavior.js:15
    call @ aurelia-binding.js:4852
    callSubscribers @ aurelia-binding.js:304
    notify @ aurelia-binding.js:3849
    handleEvent @ aurelia-binding.js:3855


  Uncaught TypeError: Cannot read property 'postcode' of null
    at Address.app / components / clients / address / address.Address.suburbSelected(address.ts:126)
    at CallScope.evaluate(aurelia - binding.js:1524)
    at Call.callSource(aurelia - binding.js:4961)
    at AubsTypeaheadCustomElement.<anonymous>(aurelia - binding.js:4985)
    at AubsTypeaheadCustomElement.itemSelected(aubs - typeahead.js:356)
    at CallScope.evaluate(aurelia - binding.js:1524)
    at Listener.callSource(aurelia - binding.js:5113)
    at Listener.handleEvent(aurelia - binding.js:5122)
    at HTMLDocument.handleDelegatedEvent(aurelia - binding.js:3237)

这是郊区的预先输入:

    let suburbLookup = fetch("/api/selectData/QuerySuburbs" + queryString, {
        method: "GET",
        headers: headers
    })
        .then(response => {
            return response.json();
        })
        .then(addressLocation => {
            if (this.address.addressLocation == null) {
                console.log("ADDRESSLOCATION BEFORE: ", this.address.addressLocation)
                this.address.addressLocation = new AddressLocation;
                console.log("ADDRESSLOCATION AFTER: ", this.address.addressLocation)
            }
                return addressLocation;
        })
        .then(addressLocation => filter.length > 0 ? addressLocation.filter(item => item.suburb.toLowerCase().indexOf(filter.toLowerCase()) > -1) : addressLocation)
        .then(addressLocation => limit ? addressLocation.splice(0, limit) : addressLocation);  // Not really needed - its done on the server.
    //.then(suburbs => console.log("Suburbs: ", suburbs));

    //this.postCode = this.address.postcode;

    console.log("suburbLookup", suburbLookup);
    return suburbLookup
}

这是包含所有属性的预输入:

                    <aubs-typeahead data.call="getSuburbData(filter, limit)"
                                    value.bind="address.addressLocation"
                                    debounce.bind="350"
                                    placeholder="Suburb..."
                                    open-on-focus.bind="true"
                                    key="suburb"
                                    results-limit.bind="10"
                                    select-single-result.bind="true"
                                    on-select.call="suburbSelected(item)"
                                    id="suburbAutocomplete">
                    </aubs-typeahead>

你会注意到有一个属性叫做on-select.call="suburbSelected(item)"

它在发生错误的郊区选择(项目)中。这是那个函数:

suburbSelected(item) {
      console.log("BEFORE ASSIGNMENT OF SUBURB NAME: ", item, item.postcode, this.address.addressLocation.postcode) //THIS IS LINE 126...
      if (item) {
        this.address.addressLocation.postcode = item.postcode;
      } else {
          this.address.addressLocation.postcode = "-";
      }
  }

在函数suburbSelected(item)运行之前,如何智能地等待promise完成?如果它等待返回的承诺,我相信它不会出错......有没有一种很好的 Typescript/Javascript 方法可以让这个函数等待另一个承诺解决??

【问题讨论】:

    标签: javascript typescript aurelia


    【解决方案1】:

    从您发布的代码中不清楚,如何调用 fetch,但我相信您可以通过 async/await 回避问题

       async getData(filter:any){  // your fetch data method
            let result = await Promise.resolve({a:'b'})  // magic happens here
    
            return result
        }
    

    【讨论】:

    • 对此仍然很新。所以下拉菜单发生,然后您选择一个选项并在它引发错误时tahts。如何添加这段代码..我是否将其添加到郊区选择项功能等它由预先输入自动调用...感谢您的回复。
    • 我在 fetch 函数中添加了 async 并在实际 fetch 中添加了 await ,但我仍然收到错误“address.ts:130 Uncaught TypeError: Cannot set property 'postcode' of null” - 就是这样与郊区Selected(item) 函数相关联
    • 似乎 .addressLocation 为空,您可以在 select 调用的函数内放置 debugger; 吗?看看你有什么?另外,你为什么使用 .call ?
    猜你喜欢
    • 1970-01-01
    • 2021-08-15
    • 2020-10-11
    • 1970-01-01
    • 2020-04-30
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    相关资源
    最近更新 更多