我正在使用此方法来查询计算的距离是否小于限制。然后它将执行此查询方法传递回request.html页面。或者,如果只有计算的距离小于限制,那么还有其他方法来执行查询,然后它会将查询传回request.html页面。
this.getRequest = this.angFire.list('request', {
query: {
orderByChild: 'reqdetails',
startAt: 'reqdetails'
}
})
我收到此错误:
我的openMapPage()方法:
openMapPage()
{
// GETTING THE CURRENT USER ADDRESS FOR LATITUDE AND LONGTITUDE
var uid = firebase.auth().currentUser.uid;
var ref = firebase.database().ref("request/" + uid);
ref.once("value").then((snapshot) => { // <------ Here!
var a = snapshot.exists(); // true
var c = snapshot.hasChild("reqdetails"); // true
var d = snapshot.child('reqdetails').exists();
var requestsKey = snapshot.key;
var requestsValue = snapshot.val();
ref.once('value', (request) => {
var currentUserAddress = request.val().regdetails.address;
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': currentUserAddress}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
this.latlng = new google.maps.LatLng(latitude, longitude);
//console.log("HAHAH current user");
//var meterLimit = this.latlng;
//var userAddress = new LatLng(currentUserAddress);
//console.log("SURESH IS COOL");
}
});
});
//END OF CURRENT USER
});
// GETTING THE ALL USER ADDRESS FOR LATITUDE AND LONGTITUDE
var ref1 = firebase.database().ref("request");
ref1.once("value").then((snapshot1) => { // <------ Here!
var a = snapshot1.exists(); // true
var c = snapshot1.hasChild("reqdetails"); // true
var d = snapshot1.child('reqdetails').exists();
var requestsKey = snapshot1.key;
var requestsValue = snapshot1.val();
snapshot1.forEach((childSnapshot) => { // <------ And here!
var requestKey = childSnapshot.key;
var requestValue = childSnapshot.val();
var reqdetails = requestValue.reqdetails;
var AllUserAddress = requestValue.regdetails.address;
var geocoder1 = new google.maps.Geocoder();
geocoder1.geocode( { 'address': AllUserAddress}, function(results, status) {
var limit = 10;
if (status == google.maps.GeocoderStatus.OK) {
var latitude1 = results[0].geometry.location.lat();
var longitude1 = results[0].geometry.location.lng();
this.latlng1 = new google.maps.LatLng(latitude1, longitude1);
//var userAddress = new LatLng(currentUserAddress);
//console.log(latlng1);
var dist: number = parseInt((google.maps.geometry.spherical.computeDistanceBetween(this.latlng,this.latlng1) / 1000).toFixed(2));
if(dist < limit)
{
console.log(dist);
console.log(AllUserAddress);
this.getRequest = this.angFire.list('request', {
query: {
orderByChild: 'reqdetails',
startAt: 'reqdetails'
}
})
}
}
});
});
});
}
我的request.html页面
我的构造函数:
constructor(public navCtrl: NavController, public navParams: NavParams, private angFire: AngularFireDatabase,private af: AngularFireDatabase,
private afAuth: AngularFireAuth) {
}
答案 0 :(得分:1)
查看您的构造函数,您的班级应该存在angFire
。这意味着唯一的解释是this
不会引用您的班级。
查看引发错误的代码,它被封装在一个回调函数中,您不使用typescript符号。如果你声明你的回调函数类似linq,你可能会得到对你的类的正确引用:
// change this:
geocoder1.geocode( { 'address': AllUserAddress}, function(results, status) {
// 'this' references the callback function
// to this:
geocoder1.geocode( { 'address': AllUserAddress}, (results, status) => {
// 'this' now references the class
答案 1 :(得分:0)
请勿在代码中直接使用this
关键字。尝试使用方法
var self = this;
在任何地方使用此行
self.getRequest = self.angFire.list