【发布时间】: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