Reputation: 75
I'm adding user geopoints and I'd like to check if snapShot.data().location.geopoint is null before pushing the data onto the array but I keep getting the following error:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'geopoint' of undefined
TypeError: Cannot read property 'geopoint' of undefined
Here is the actual code?
query.get()
.then(list => {
list.forEach(snapShot => {
let userLat = (snapShot.data().location.geopoint.latitude > 0) ? snapShot.data().location.geopoint.latitude : 0;
let userLong = (snapShot.data().location.geopoint.longitude > 0) ? snapShot.data().location.geopoint.longitude : 0;
console.log('use lat long', userLat, userLong);
let distance = this.geo.distance(this.geo.point(myLat, myLong), this.geo.point(userLat, userLong));
if (distance <= this.userSettings.distance.upper) {
this.userInfo.push({
userId: snapShot.data().userId,
displayName: snapShot.data().displayName,
});
}
});
})
how would I check for a null value in geopoint given it's complaining about that actual null check I added:
let userLat = (snapShot.data().location.geopoint.latitude > 0) ? snapShot.data().location.geopoint.latitude : 0;
Upvotes: 0
Views: 150
Reputation: 26313
You could make use of the snapshot.get(fieldPath)
API (docs) and do:
let userLat = 0;
let userLong = 0;
if (snapShot.get("location.geopoint")) {
userLat = snapShot.get("location.geopoint").latitude;
userLong = snapShot.get("location.geopoint").longitude;
}
Upvotes: 3