从角度2中返回geofire的承诺

时间:2018-04-13 05:21:10

标签: angular typescript angular-promise geofire

我正在尝试从使用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);

我甚至不明白这一点。 请告知我缺少的东西。

1 个答案:

答案 0 :(得分:0)

您的getLocations本身并未返回任何内容。

return geoQuery.on的{​​{1}}声明,因为您正在呼叫getLocations(),因此会发出错误。