【问题标题】:returning promise from geofire in angular 2从角度2的geofire返回承诺
【发布时间】:2018-04-13 05:21:10
【问题描述】:

我正在尝试从使用 geofire 的函数返回一组自定义对象。请注意,geofire 具有异步事件方法。

  getLocations(radius: number, coords: Array<number>) : Promise<Vendor[]>{
    console.log('In get locations');
    const geoQuery = this.geoFire.query({
      center: coords,
      radius: radius
    });
    const onKeyEnteredRegistration = geoQuery.on('key_entered', (key, location, distance) => {
        console.log(' key '+key+' distance '+distance);
        this.VendorRef = new Vendor();
        this.VendorRef.uid = key;
        console.log('current count '+this.VendorsNEarBy.push(this.VendorRef));
    });
    const onReadyRegistration = geoQuery.on('ready', ()=>{
        console.log("GeoQuery has loaded and fired all other events for initial data");
        console.log('returning VendorsNEarBy');
        return new Promise<Vendor[]>(this.VendorsNEarBy);
    });
   }

调用代码是

this.geo.getLocations(100, [this.CustRef.latitude, this.CustRef.longitude])
    .then((tempList) =>{
        this.VendorList = tempList;
        console.log('this VendorList data '+this.VendorList);
    })
    .catch((error) => {
        console.log('some error')
    });

这两个,我得到如下错误。

[10:38:58]  typescript: providers/geofire/geofire.ts, line: 38
            A function whose declared type is neither 'void' nor 'any' must return a value.

      L38:    getLocations(radius: number, coords: Array<number>) : Promise<Vendors[]>{

但我要返回 onReadyRegistration,对吗?

第二个错误是,

[10:38:58]  typescript: /providers/geofire/geofire.ts, line: 59
            Argument of type 'Vendor[]' is not assignable to parameter of type '(resolve: (value?: Vendor[] |
            PromiseLike<Vendor[]>) => void, reject: (reason?: any) => void) =...'. Type 'Vendor[]' provides no match
            for the signature '(resolve: (value?: Vendor[] | PromiseLike<Vendor[]>) => void, reject: (reason?: any) =>
            void): void'.

      L58:  console.log('returning VendorsNearBy');
      L59:  return new Promise<Vendor[]>(this.VendorsNearBy);

我什至无法理解这一点。 请就我缺少的内容提出建议。

【问题讨论】:

  • 我的回答对你有用吗?

标签: angular typescript angular-promise geofire


【解决方案1】:

您的getLocations 本身没有返回任何内容。

您有 geoQuery.onreturn 语句,当您调用 getLocations() 时,它会给出该错误。

【讨论】:

    猜你喜欢
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 2017-03-16
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多