问题描述
const getEmergencies = async (body) => {
const {
responderId,latitude: responderLatitude,longitude: responderLongitude,} = body;
// Store all filtered emergencies in here
const filteredEmergencies = [];
// Find emergencies within 10km of the location of the responder
const center = [responderLatitude,responderLongitude];
const radiusInM = 10 * 1000; // 10km
// Each item in 'bounds' represents a startAt/endAt pair. We have to issue
// a separate query for each pair. There can be up to 9 pairs of bounds
// depending on overlap,but in most cases there are 4.
const bounds = geofire.geohashQueryBounds(center,radiusInM);
const promises = [];
bounds.forEach((bound) => {
const query = db
.collection('emergencies')
.orderBy('location.geohash')
.startAt(bound[0])
.endAt(bound[1]);
promises.push(query.get());
});
// Collect all the query results together into a single list
await Promise.all(promises)
.then((snapshots) => {
const emergencies = [];
snapshots.forEach((snapshot) => {
snapshot.docs.forEach((emergency) => {
// This is the location of the emergency
const { latitude,longitude } = emergency.get('location');
// We have to filter out all emergencies that are
// > 10km away from the responder
const distanceInKm = geofire.distanceBetween(
[latitude,longitude],center
);
const distanceInM = distanceInKm * 1000;
if (distanceInM <= radiusInM) {
emergencies.push({ emergency,distance: distanceInKm });
}
});
});
return emergencies;
})
.then(async (emergencies) => {
await Promise.all(
emergencies.map(async ({ emergency,distance }) => {
const emergencyData = emergency.data();
// We need to filter out emergencies if:
// 1. Number of Responders is less than or equal to 3
// 2. Status is ongoing
if (
emergencyData.numberOfResponders <= 3 &&
emergencyData.status === 'ongoing'
) {
// Additional filter for emergencies is to see
// if the emergency was not declined by the responder
const declinedRespondeRSSnapshot = await emergency.ref
.collection('declinedResponders')
.where('responderId','==',responderId)
.get();
// If the result is empty,then that means that
// the responder has not declined this emergency.
// So,we push this emergency to our filteredEmergencies array.
if (declinedRespondeRSSnapshot.empty) {
filteredEmergencies.push({
...emergencyData,emergencyId: emergency.id,distance,});
}
}
})
);
});
socket.emit('emergencies:get',filteredEmergencies);
};
我想将从此 API 创建的新“紧急情况”实时发送到客户端。我一直在寻找有关如何执行此操作的一段时间,但似乎找不到与我的情况类似的任何内容。我是否需要重构我的代码才能达到预期的结果?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)