error: cannot read property 'latitude' of undefined in ionic 4 and firebase

error: cannot read property 'latitude' of undefined in ionic 4 and firebase

我正在尝试使用 ionic 4 和 firebase 构建一个应用程序。有两种类型的用户:简单用户和管理员。作为管理员,我想在地图上查看用户所在的位置,但仅限于特定区域。我能够在地图上显示我的位置并使用 watchPosition() 更新标记。我在我的数据库中添加了一些位置供简单用户查看我是否可以访问它们然后将它们显示在地图上。一切正常,标记显示在地图上,但现在我收到此错误:无法读取未定义的 属性 'latitude',但我没有更改我的代码中的任何内容(我访问的位置用户)这很奇怪,因为几天前它运行良好,并且在 android 上也运行良好。 这是来自 firebase 还是来自地理定位的问题,有人知道吗?

这是我的代码:

 getMarkers() {

let usersLocations =[];
firebase
  .firestore()
  .collection("users")
  .get()
  .then(userProfileSnapshot => {
      userProfileSnapshot.docs.forEach(doc => {
        if (doc.data().isAdmin==false)
        usersLocations.push(doc.data());
      });
  }).then((x) => {
    if(this.isAdmin==true)
    for(let i=0; i<usersLocations.length; i++) {
      let userPosition = new google.maps.LatLng(usersLocations[i].Location.latitude, usersLocations[i].Location.longitude);
      let userMarker = new google.maps.Marker({
        position: userPosition,
        icon: {
          url: "http://maps.google.com/mapfiles/ms/icons/yellow-dot.png"
        },
        map: this.map
      });

  //     if(this.haversine_distance(this.locationMarker, userMarker)>1)
  //       userMarker.setVisible(false);
    }
  });

}

从您共享的代码中我们只能说,对于 i 的某些值,usersLocations[i] 没有 Location 属性。如果您没有更改代码,则需要检查 属性.

缺少哪个文档

或者,您可以跳过此类文档并通过将第二个 then 更改为:

来记录它们的 ID
for(let i=0; i<usersLocations.length; i++) {
  if (!usersLocations[i] || !usersLocations[i].Location) {
    console.error("Missing Location data in "+i+": "+JSON.stringify(usersLocations[i]));
    continue; // skip further processing of this user
  }
  let userPosition = new google.maps.LatLng(usersLocations[i].Location.latitude, usersLocations[i].Location.longitude);
  let userMarker = new google.maps.Marker({
    position: userPosition,
    icon: {
      url: "http://maps.google.com/mapfiles/ms/icons/yellow-dot.png"
    },
    map: this.map
  });
}

一种稍微 modern/idiomatic 的方法是:

firebase
  .firestore()
  .collection("users")
  .get()
  .then(querySnapshot => {
    // Convert the snapshot to an array of IDs and data
    return querySnapshot.docs.map(doc => { doc.id, ...doc.data() });
  })
  .then(documents => {
    // Remove admins
    return documents.filter(doc => doc.isAdmin==false);
  })
  .then(documents => {
    // Remove and log documents that have no location field
    return documents.filter(doc => {
      if (!doc.Location) {
        console.error(`Document ${doc.id} doesn't have a Location`);
        return false; // remove this document from the array
      }
      return true;
    });
  .then(documents => {
    documents.forEach(doc => {
      let userPosition = new google.maps.LatLng(doc.Location.latitude, doc.Location.longitude);
      let userMarker = new google.maps.Marker({
        position: userPosition,
        icon: {
          url: "http://maps.google.com/mapfiles/ms/icons/yellow-dot.png"
        },
        map: this.map
      });
    });
  });