angular 2 中来自 geofire 的返回承诺

returning promise from geofire in angular 2

我正在尝试 return 来自使用 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[]>{

但我正在 returnonReadyRegistration,对吗?

第二个错误是,

[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);

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

你的 getLocations 本身没有返回任何东西。

你在调用 geoQuery.on 的地方有 return 语句,所以它给出了那个错误。