react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

Cannot read property 'ready' of null 

Open mumnanikunj opened this issue 2 years ago • 24 comments

@christocracy

Your Environment

•	Plugin version: react-native-background-geolocation : 4.14.1 , react-native-background-fetch : 4.2.1
•	Platform: Android
•	OS version: Android  12
•	Device manufacturer / model: simulator
•	React Native version (react-native -v): 0.68.2
•	Plugin config : build.gradle and app/bulid.gradle
 this.subscriptions.push(BackgroundGeolocation.onLocation(async (location) => {

            try {
              let oldLatLong = await AsyncStorage.getItem("LOCATION_LAT");
              // let odometer = await BackgroundGeolocation.getOdometer();
              console.log("LOCA ", oldLatLong);
              let meterDistance = 0;
      
              if (oldLatLong && oldLatLong != null) {
      
                oldLatLong = oldLatLong.split('|');
                let oldLat = oldLatLong[0]
                let oldLong = oldLatLong[1]
                let today = moment().format('MM/DD/YYYY');
      
                if (moment(today, 'MM/DD/YYYY').isSame(moment(oldLatLong[2], 'MM/DD/YYYY'), 'day') == true) {
                  const a = { latitude: oldLat, longitude: oldLong }
                  const b = { latitude: location?.coords?.latitude, longitude: location?.coords?.longitude }
                  meterDistance = haversine(a, b);
                }
                else {
                  await AsyncStorage.setItem("LOCATION_LAT", location?.coords?.latitude + '|' + location?.coords?.longitude + '|' + moment().format('MM/DD/YYYY'))
                }
              }
              else {
                await AsyncStorage.setItem("LOCATION_LAT", location?.coords?.latitude + '|' + location?.coords?.longitude + '|' + moment().format('MM/DD/YYYY'))
              }
      
              this.setState({ location: JSON.stringify(location, null, 2) });
      
              const format = 'hh:mm:ss';
              const beforeTime = moment('22:50:00', format);
              const afterTime = moment('24:00:00', format);
              if (moment().isBetween(beforeTime, afterTime)) {
                await AsyncStorage.removeItem("LOCATION_LAT")
                BackgroundGeolocation.stop();
                this.setState({ enabled: 0 })
              }
      
              let callAPI = false;
              if (meterDistance > 49) {
                callAPI = true;
              }
              console.log("meterDistance ", meterDistance);
      
              if (callAPI) {
                console.log("callAPI ", callAPI);
      
                let header = {
                  Accept: "application/json",
                  "Content-Type": "multipart/form-data"
                };
      
                let latilong = location?.coords?.latitude + ',' + location?.coords?.longitude;
      
                let batteryPercentage = location?.battery?.level * 100
                batteryPercentage = batteryPercentage.toString()
      
                let storeLocationData = [{
                  "distance": meterDistance.toString(),
                  "lat_long": latilong,
                  "battery_percentage": batteryPercentage,
                  "location_time": moment().format("DD/MM/YYYY HH:mm:ss"),
                  "fk_user_id": User_Id
                }];
      
                fetch(`not`, {
                  method: "POST",
                  headers: header,
                  body: JSON.stringify(storeLocationData)
                })
                  .then(response => response.json())
                  .then(async responseJson => {
                    this.setState({
                      lastAPI: moment().format("DD/MM/YYYY HH:mm:ss"),
                      meter: meterDistance
                    });
                    await AsyncStorage.setItem("LOCATION_LAT", location?.coords?.latitude + '|' + location?.coords?.longitude + '|' + moment().format('MM/DD/YYYY'))
                    BackgroundGeolocation.resetOdometer().then((location) => {
                      // This is the location where odometer was set at.
                      console.log("[setOdometer] success: ", location);
                    });
      
                  })
                  .catch(error => {
                    console.error(error);
                  });
              }
            } catch (error) {
              console.log("er ", error);
            }
          }))
      
      
          this.subscriptions.push(BackgroundGeolocation.onMotionChange((event) => {
            console.log('[onMotionChange]', event);
          }))
      
          this.subscriptions.push(BackgroundGeolocation.onActivityChange((event) => {
            console.log('[onActivityChange]', event);
          }))
      
          this.subscriptions.push(BackgroundGeolocation.onProviderChange((event) => {
            console.log('[onProviderChange]', event);
          }))
          
          console.log('Background.Ready==>>')
          /// 2. ready the plugin.
          BackgroundGeolocation.ready({
            // Geolocation Config
            desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
            distanceFilter: 50,
            showsBackgroundLocationIndicator: false,
            foregroundService: true,
            enableHeadless: true,
            // Activity Recognition
            stopTimeout: 5,
            maxDaysToPersist: 14,
            //heartbeatInterval: 60,
            locationUpdateInterval: 5000,  // Get a location every 5 seconds
            // Application config
            debug: false, // <-- enable this hear sounds for background-geolocation life-cycle.
            logLevel: BackgroundGeolocation.LOG_LEVEL_ERROR,
            stopOnTerminate: false,   // <-- Allow the background-service to continue tracking when user closes the app.
            startOnBoot: true,        // <-- Auto start tracking when device is powered-up.
            foregroundService: true,
            //   desiredOdometerAccuracy: 10,
})

Expected Behavior

working background location services and track Device

Actual Behavior

Not work it appears an error on console: TypeError: Cannot read property 'ready' of null

Steps to Reproduce

Context

trying to tracking device with library

BackgroundGeolocation.ready({ // Geolocation Config desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH, distanceFilter: 50, showsBackgroundLocationIndicator: false, foregroundService: true, enableHeadless: true, // Activity Recognition stopTimeout: 5, maxDaysToPersist: 14, //heartbeatInterval: 60, locationUpdateInterval: 5000, // Get a location every 5 seconds // Application config debug: false, // <-- enable this hear sounds for background-geolocation life-cycle. logLevel: BackgroundGeolocation.LOG_LEVEL_ERROR, stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app. startOnBoot: true, // <-- Auto start tracking when device is powered-up. foregroundService: true, // desiredOdometerAccuracy: 10, // url: '', // HTTP / SQLite config batchSync: false, // <-- [Default: false] Set true to sync locations to server in a single HTTP request. autoSync: true, // <-- [Default: true] Set true to sync each location to server as it arrives. headers: { // <-- Optional HTTP headers // Accept: "application/json", // "Content-Type": "multipart/form-data" },

        // params:
        // {              // <-- Optional HTTP params
        //   "fk_user_id": userid,
        //   "location_time": moment().format("DD/MM/YYYY HH:mm:ss"),
        // }
      }).then(async (state) => {
        this.setState({ enabled: state.enabled });
      });

Debug logs

Logs
PASTE_YOUR_LOGS_HERE

mumnanikunj avatar Oct 05 '23 09:10 mumnanikunj

Your app thinks the plug-in is not installed. Revisit readme and Setup Instructions.

christocracy avatar Oct 05 '23 12:10 christocracy

=> i am already done all Setup Instructions and all things => working on Demo Project Fine but Current Project not working properly => Please tell which Other Plugin are Require

mumnanikunj avatar Oct 05 '23 12:10 mumnanikunj

Everything required is in the Setup Instructions.

christocracy avatar Oct 05 '23 12:10 christocracy

can you check my steps ? i am send the code here

mumnanikunj avatar Oct 05 '23 12:10 mumnanikunj

I suggest you terminate your dev server.

christocracy avatar Oct 05 '23 12:10 christocracy

Your code has nothing to do with your app not being aware of the plug-in.

christocracy avatar Oct 05 '23 12:10 christocracy

if posible to license key are use multiple project and not working other project ?

mumnanikunj avatar Oct 05 '23 12:10 mumnanikunj

i mean can you check my Plugins ? setup

mumnanikunj avatar Oct 05 '23 12:10 mumnanikunj

I have no idea what that means

christocracy avatar Oct 05 '23 12:10 christocracy

can you check my require step for setup because i am setup properly but i did't understand what is the issue

mumnanikunj avatar Oct 05 '23 13:10 mumnanikunj

I have no idea what your issue is either. I suggest you terminate your dev server.

christocracy avatar Oct 05 '23 13:10 christocracy

i am create new demo and implement your library setup properly and it's working no issue but if i use in my Current project as it is setup your document that is not working so tell how can i fix it

mumnanikunj avatar Oct 05 '23 13:10 mumnanikunj

getting me Cannot read property 'ready' of null and Cannot read property 'start' of null those are warning in console

mumnanikunj avatar Oct 05 '23 13:10 mumnanikunj

getting me Cannot read property 'ready' of null and Cannot read property 'start' of null those are warning in console

Yes, that's exactly what your issue title stated.

so tell how can i fix it

I have no idea what your problem is and I cannot tell you how to fix it. The problem is on your end, not mine.

christocracy avatar Oct 05 '23 13:10 christocracy

but if i create new demo and implement it's working

mumnanikunj avatar Oct 05 '23 13:10 mumnanikunj

Yes, you stated that already. I have no idea what your problem is or how to fix it. The problem is on your end.

christocracy avatar Oct 05 '23 13:10 christocracy

can you check my Environment, Context ?

mumnanikunj avatar Oct 05 '23 13:10 mumnanikunj

and one more thing if add this line in setting.gradle include ':react-native-background-geolocation' project(':react-native-background-geolocation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-geolocation/android')

include ':react-native-background-fetch' project(':react-native-background-fetch').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-fetch/android')

so fixed this error Cannot read property 'ready' of null and Cannot read property 'start' of null but i have other error Cannot read property 'RNBackgroundGeolocation'

mumnanikunj avatar Oct 05 '23 13:10 mumnanikunj

Your app is corrupt. Re-generate a fresh new RN app and migrate all your /src and dependencies into the new app.

christocracy avatar Oct 05 '23 13:10 christocracy

if i try this new RN app and migrate all your /src and dependencies into the new app. but create new RN app create new package name and purchase new key any my current project live in playstore and appstore so this idea is bullshit

mumnanikunj avatar Oct 05 '23 13:10 mumnanikunj

Obviously you'd use the same applicationId in the newly generated app as your existing app.

The problem is in your own app. This problem is not caused by the plugin.

christocracy avatar Oct 05 '23 13:10 christocracy

I just did this myself in the SampleApp a few weeks ago.

Use react-native-rename to rename your app as desired.

christocracy avatar Oct 05 '23 13:10 christocracy

Okay thank you if i fix this issue and i will inform u thanks for instant reply

mumnanikunj avatar Oct 05 '23 13:10 mumnanikunj

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar May 03 '24 01:05 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar May 18 '24 01:05 github-actions[bot]

I am also facing the same issuse it is an expo cli application

Naveenkumar-1411 avatar Jul 17 '24 10:07 Naveenkumar-1411

make sure to rebuild app after installing the package npx react-native run-android

Sulaiman122 avatar Aug 19 '24 09:08 Sulaiman122