gpt4 book ai didi

javascript - 如何检查 firebase data() 中地理点字段上的空值

转载 作者:行者123 更新时间:2023-12-02 21:01:41 24 4
gpt4 key购买 nike

我正在添加用户地理点,我想在将数据推送到数组之前检查 snapShot.data().location.geopoint 是否为空,但我不断收到以下错误:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'geopoint' of undefined
TypeError: Cannot read property 'geopoint' of undefined

这是实际的代码吗?

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,
});
}
});
})

鉴于它提示我添加的实际空值检查,我将如何检查地理点中的空值:

            let userLat = (snapShot.data().location.geopoint.latitude > 0) ? snapShot.data().location.geopoint.latitude : 0;

最佳答案

您可以使用 snapshot.get(fieldPath) API ( docs ) 并执行以下操作:

let userLat = 0;
let userLong = 0;

if (snapShot.get("location.geopoint")) {
userLat = snapShot.get("location.geopoint").latitude;
userLong = snapShot.get("location.geopoint").longitude;
}

关于javascript - 如何检查 firebase data() 中地理点字段上的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61328653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com