嘿,我是React Native的新手,我有一个应用程序,可以从Firebase那里获取数据,直到到达我应该获取数据的屏幕,然后按名称或距离名称对它进行排序都很好,但与距离相比,它给了我一个异常甚至是地理定位有时都行得通,有时没有,我删除浏览器缓存,删除应用程序,删除手机缓存,但仍然是同一个问题
@firebase/database: FIREBASE WARNING: Exception was thrown by user callback. Error: 'undefined' is no valid coordinate.
firebase.database().ref().child('businesses')
.orderByChild('category')
.equalTo(`${this.state.idorga}`)
.once('value', (snapshot) => {
const data = snapshot.val();
// const items = Object.values(data);
const items = Object.keys(data).reduce((result, key) =>
result.concat({ id: key, ...data[key] }), []);
const data1 = items;
//here the code who sort my data with name
if (this.state.tri !== 'alphabetique') {
let i = 0;
while (i < data1.length - 1) {
if (data1[i].name.localeCompare(data1[i + 1].name) === 1) {
const a = data1[i];
data1[i] = data1[i + 1];
data1[i + 1] = a;
i = 0;
} else {
i++;
}
}
} else {
//here where i try to sort my data using distance
let i = 0;
navigator.geolocation.getCurrentPosition((position) => this.setState({ location: position }));
while (i < data1.length - 1) {
// here i am using geolib to calcule disttance and store it on a const
const A = geolib.getDistance(this.state.location.coords, {
latitude: data1[i].mapdata.annotations[0].latitude,
longitude: data1[i].mapdata.annotations[0].longitude,
});
const B = geolib.getDistance(this.state.location.coords, {
latitude: data1[i + 1].mapdata.annotations[0].latitude,
longitude: data1[i + 1].mapdata.annotations[0].longitude,
});
if (A > B) {
const a = data1[i];
data1[i] = data1[i + 1];
data1[i + 1] = a;
i = 0;
} else {
i++;
}
}
}
this.setState({ items: data1 });
this.setState({ dataBackup: data1 });
});
}