2017-07-27 133 views
0

如何使用离子3.5火力使用geofire离子3, 林正道: “4.1.3”, “geofire”: “4.1.2”, “angularfire2”:“4.0。 geofire.d.ts的0-RC.1" ,离子3 geofire angularfire2

constructor(public angularfireDatabase: AngularFireDatabase, 
private geolocation: Geolocation) { } 



ionViewDidLoad() { 
    this.geolocation.getCurrentPosition().then((resp) => 
    this.geoQuery= this.geoFire.query({ 
    center: [resp.coords.latitude, resp.coords.longitude], 
    radius: 20 //kilometers 
    });    
    this.geoQuery.on("key_entered", function (key, location, distance) { 
     console.log('from geofire ' +location, ' key ' + key,distance); 
     this.angularfireDatabase.object('/products/'+key).subscribe((product) => { 
      this.products.push(product); 
     }); 
    }); 
    }).catch((error) => { 
    console.log('Error getting location', error); 
}); 
} 

返回该错误

ERROR TypeError: Cannot read property 'angularfireDatabase' of undefined 
at products.ts:76 
at geofire.js:685 

内容

interface GeoQuery { 
    center(): number[]; 
    radius(): number; 
    updateCriteria(criteria: GeoQueryUpdateCriteria); 
    on(eventType: EventType, callback: (key:string, location: number[], distance: number) => void): GeoCallbackRegistration; 
    cancel(); 
} 
class GeoFire { 
    constructor(ref: any); 
    ref(): any; 
    set(key: string, loc: number[]): Promise<void>; 
    get(key: string): Promise<number[]>; 
    remove(key: string): Promise<void>; 
    query(criteria: GeoQueryCriteria): GeoQuery; 
    static distance(location1: number[], location2: number[]); 
} 

如何申报打字稿的正确方法?。简单例子还没有工作

public pubVar:any; 
    constructor(public angularfireDatabase: AngularFireDatabase, 
    private geolocation: Geolocation) { } 



    ionViewDidLoad() { 
    this.pubVar = 'hii'; 
    this.geolocation.getCurrentPosition().then((resp) => 
     this.geoQuery= this.geoFire.query({ 
     center: [resp.coords.latitude, resp.coords.longitude], 
     radius: 20 //kilometers 
     });    
    this.geoQuery.on("key_entered", function (key, location, distance) { 
     console.log('from geofire ' +location, ' key ' + key,distance); 
     console.log(this.pubVar); 


    }).catch((error) => { 
    console.log('Error getting location', error); 
    }); 
} 

也返回

Uncaught TypeError: Cannot read property 'pubVar' of undefined 
+0

https://github.com/daveozoalor/geofire-firebase-not-working-in -ionic修复 – Mustafa

回答

0

关键字“这个”没有定义或约束回调的内部,它回来的未定义。如果您使用的脂肪箭头符号,与key_entered事件“这”将是范围内的回调:使用

this.geolocation.getCurrentPosition().then((resp) => 
    this.geoQuery= this.geoFire.query({ 
    center: [resp.coords.latitude, resp.coords.longitude], 
    radius: 20 //kilometers 
    });    
    this.geoQuery.on("key_entered", (key, location, distance) => { 
     console.log('from geofire ' +location, ' key ' + key,distance); 
     this.angularfireDatabase.object('/products/'+key).subscribe((product) => { 
      this.products.push(product); 
     }); 
    }); 
    }).catch((error) => { 
    console.log('Error getting location', error); 
});