Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Gabotronix's avatar

TypeError: _geolocationApi.default.requestLocationPermissions is not a function

in my react native app I have a file where I store different functions to get user coordinates so that I don't cram my components and to keep logic separate, however when I try to call any of my functions from another file I get this error:

TypeError: _geolocationApi.default.requestLocationPermissions is not a function.

In my component I call it like this (simplified):

import geolocationApi from '@utils/geolocationApi.js'

export default LoginCard1 = () => 
{

let result = await geolocationApi.requestLocationPermissions(true);

/* ... */

};

Then my geolocationApi.js file

/utils/geolocationApi.js



import { AsyncStorage ,Platform, Alert, PermissionsAndroid } from 'react-native';
import _ from 'lodash';
import env from '@env/vars';
import http from '@env/axiosin';
import Geolocation from 'react-native-geolocation-service';

import { useStore } from '@hooks/use-store';


const geolocationApi = () => {
    const root = useStore();
    
    


    const requestLocationPermissions = async (getCityName = false) =>
    {
        console.log('getting new coordinates');
        if (Platform.OS === 'ios')
        {
            const auth = await Geolocation.requestAuthorization("whenInUse");
            if(auth === "granted")
            {
                //root.mapStore.setLocationEnabled(true);
                this.locationEnabled = true;
                let coords = await getMe(getCityName);
                return coords;
                /*
                const todayId = moment().isoWeekday();
                if(todayId != root.userStore.user.lastLoginDayId)
                {
                    getMe();
                }
                */
                
            }
            else
            {
                //Alert.alert('PLEASE ENABLE LOCATION');
                root.mapStore.setLocationEnabled(false);
                //this.locationEnabled = false;
            }
        }
        if (Platform.OS === 'android')
        {
            let granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
            if (granted === PermissionsAndroid.RESULTS.GRANTED) 
            {
            // do something if granted...
            
                //this.loactionEnabled = true;
                root.mapStore.setLocationEnabled(true);
                let coords = await getMe();
                return coords;
                /*
                const todayId = moment().isoWeekday();
                if(todayId != root.userStore.user.lastLoginDayId)
                {
                    getMe();
                }
                */
            }
            else
            {
                //Alert.alert('KULO');
                root.mapStore.setLocationEnabled(false);
                //this.locationEnabled = false;
            }
        }
    }

    const getMe = async () =>
    {
        if (hasLocationPermission) {
            //Alert.alert('KAKA');
            Geolocation.getCurrentPosition(
                async (position) => {
                  //console.log(position);
                  let results = await onSuccess(position.coords);
                  return results;
                },
                (error) => {
                  // See error code charts below.
                  console.log(error.code, error.message);
                },
                { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
            );
    }}

    const onSuccess = async (coords) =>
    {
        //Alert.alert('KAKA');
        let newCity = false;
        let currentCoordinates = { lat:coords.latitude, lng:coords.longitude };
        console.log('currentCoordinatesOnSuccess',currentCoordinates);
        //Alert.alert(currentCoordinates.lat+' '+currentCoordinates.lng);
                root.mapStore.setCurrentCoordinates(currentCoordinates);
                //this.currentCoordinates = currentCoordinates;
                //this.setCurrentCoordinates(currentCoordinates);
                //console.log('currentCoordinates',root.mapStore.currentCoordinates);
                if(root.userStore.user)
                {
                    root.userStore.setLat(currentCoordinates.lat);
                    root.userStore.setLng(currentCoordinates.lng);
                }
               
                if(root.mapStore.city == '' || getCityName)
                {
                    let params =
                    {
                        latlng : currentCoordinates.lat+','+currentCoordinates.lng,
                        sensor : true,
                        key : env.MAPS_KEY
                    };
                    let response = await http.get('https://maps.googleapis.com/maps/api/geocode/json?', { params :params });
                    newCity = response.data.results[0].address_components[2].long_name;
                    root.mapStore.setCity(newCity);
                    //setCity(newCity);
                }
                
                
                //console.log(response);
                //Alert.alert(response.data.results[0].address_components[2].long_name);
                //root.mapStore.setStreet(response.data.results[0].address_components[1].long_name);
                //root.mapStore.setCountry(response.data.results[0].address_components[5].long_name);
                //root.mapStore.setCity(response.data.results[0].address_components[2].long_name);
                //this.city = response.data.results[0].address_components[2].long_name;
                //root.mapStore.setZipCode(response.data.results[0].address_components[6].long_name);
                //setCoordinates(currentCoordinates); 
                return { currentCoordinates, newCity };
    };


    const hasLocationPermission = async () =>
    {
        if (Platform.OS === 'ios') {
          const hasPermission = await this.hasLocationPermissionIOS();
          return hasPermission;
        }
    
        if (Platform.OS === 'android' && Platform.Version < 23) {
            Alert.alert('KAKA');
          return true;
        }
    
        const hasPermission = await PermissionsAndroid.check(
          PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        );
    
        if (hasPermission) {
          return true;
        }
    
        const status = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        );
    
        if (status === PermissionsAndroid.RESULTS.GRANTED) {
          return true;
        }
    
        if (status === PermissionsAndroid.RESULTS.DENIED) {
          ToastAndroid.show(
            'Location permission denied by user.',
            ToastAndroid.LONG,
          );
        } else if (status === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
          ToastAndroid.show(
            'Location permission revoked by user.',
            ToastAndroid.LONG,
          );
        }
    
        return false;
      };

       /*
      useEffect(() => {
        requestLocationPermissions();
      }, []);
      console.log('returning new coordinates with hook: '+coordinates);
      */
      //return { coordinates, city };
    };
    
    export default geolocationApi;
0 likes
0 replies

Please or to participate in this conversation.