【发布时间】:2020-07-20 20:30:31
【问题描述】:
我有一个从 Firebase 数据库收集位置的 for 循环。根据这些数据,我还计算出用户手机与其所在位置之间的距离。
如果找不到 userData,我的应用程序现在会中断,因此我想为不存在用户位置创建故障保护。但是我无法让它工作...... if AND else 语句中的两个 console.log 条目都不会被触发。我也没有看到“这是否有效”的外部评论。对我来说已经晚了,所以也许我错过了一些明显的东西?
for (const key in resData) {
const reduxUserLocation = getState().locationActions.userLocation
if (reduxUserLocation === null) {
const calculateDistance = getDistance(
{ latitude: resData[key].location[0].lat, longitude: resData[key].location[0].lng },
{ latitude: reduxUserLocation.lat, longitude: reduxUserLocation.lng }
)
console.log('distance?' , calculateDistance)
loadLocations.push(
new ReportedLocations(
key,
resData[key].ownerId,
resData[key].location,
resData[key].description,
resData[key].streetname,
resData[key].placename,
resData[key].date,
calculateDistance
)
);
}
else {
console.log("user location isn't found")
const calculateDistance = 0
loadLocations.push(
new ReportedLocations(
key,
resData[key].ownerId,
resData[key].location,
resData[key].description,
resData[key].streetname,
resData[key].placename,
resData[key].date,
calculateDistance
)
);
}
console.log('Does this work outside the if/else statement?')
}
【问题讨论】:
-
你有一个语法错误,在 "console.log('user location is not found')" 周围更改每个双引号的单引号,它应该可以工作
-
当看到,对我来说太晚了,我错过了那些 ;-) 没有解决问题,但谢谢!
标签: javascript react-native redux