Функция запуска Firebase внутри для каждого - PullRequest
0 голосов
/ 07 февраля 2020

Я пытаюсь использовать геолокацию, чтобы добавить только определенные горы, если они находятся внутри выбранного радиуса. Когда я пытаюсь запустить isPointWithinRadius внутри оператора forEach, ничего не возвращается. Если я удаляю оператор if (isInsideRadius) {, ничего не возвращается. Это как если бы этот оператор блокировал mountains.push независимо от того, является ли этот оператор до или после. Очень запутано, почему просто наличие функции isInsideRadius не приведет к «горному» даже в том случае, если оператора if там нет. Подумай, если я смогу понять это, тогда, может быть, я смогу понять, как все это решить.

// get all of the possible mountains for user to pick where they want to ride
export const getAllMountains = () => {
  return dispatch => {
    dispatch(fetchMountainsLoading())

    Geolocation.getCurrentPosition(async (position) => {
      const userLatitude = position.coords.latitude;
      const userLongitude = position.coords.longitude;

      return FIREBASE_REF_MOUNTAINS.once('value')
        .then((snapshot) => {
          let mountains = [];
          snapshot.forEach((channelSnapshot) => {
            let channelId = channelSnapshot.key
            let channelInfo = channelSnapshot.val()

            let isInsideRadius = isPointWithinRadius(
              { latitude: parseFloat(userLatitude), longitude: parseFloat(userLongitude) },
              { latitude: parseFloat(mountainLatitude), longitude: parseFloat(mountainLongitude) },
              1700000
            )

// here is where I have removed the if statement, but the push doesn't work
// it only works if I remove the insideRadius function
                if (isInsideRadius) {
                  mountains.push({ id: channelId, info: channelInfo })
                }
              }
              )
              return Promise.all(mountains)
            })
            .then(mountains => dispatch(fetchMountainsSuccess(mountains)))
            .catch(error => {
              dispatch(fetchMountainsFailure(error))
              reportError(error)
            })
        },
          error => dispatch(locationError(error)),
          { enableHighAccuracy: true, timeout: 20000, maximumAge: 0 }
        )
      }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...