首页 文章

无法读取未定义的属性“列表”

提问于
浏览
-1

我正在使用此方法来查询计算的距离是否小于限制。然后它将执行此查询方法传回 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) {

  }

2 回答

  • 1

    查看构造函数,angFire应存在于您的类中。这意味着唯一的解释是this不是指你的班级。

    查看引发错误的代码,它被封装在一个回调函数中,您不使用 typescript 符号。如果你声明你的回调函数 linq-like,你可能会得到你的类的正确引用:

    // 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
    
  • 0

    不要将this关键字直接用于代码。尝试使用该方法的这一行

    var self = this;
    

    在任何地方使用此行

    self.getRequest = self.angFire.list
    

相关问题