StasDoskalenko / react-native-google-fit

A React Native bridge module for interacting with Google Fit
MIT License
333 stars 209 forks source link

Getting Steps or Sleep data as [object], how to decode it ? #223

Closed patriksharma closed 3 years ago

patriksharma commented 3 years ago

I have implemented a library, Authorized is true but when I try to get Sleep data or steps data I'm getting object array and no instructions how to decode it.

"granularity": [[Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object]],

[ LOG  Authorized {"success": true}
 LOG  [{"addedBy": "com.xiaomi.hm.health", "endDate": "2021-04-15T00:26:00.000Z", "granularity": [[Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object]], "startDate": "2021-04-14T18:51:00.000Z"}, {"addedBy": "com.xiaomi.hm.health", "endDate": "2021-04-16T02:10:00.000Z", "granularity": [[Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object], [Object]], "startDate": "2021-04-15T17:34:00.000Z"},]

No idea how to decode that object to get real values

enestatli commented 3 years ago

Can you please log any of the index?

aboveyunhai commented 3 years ago

https://github.com/StasDoskalenko/react-native-google-fit/blob/fbcf2c70042150bdea86cced641a7e006eb37db5/index.android.d.ts#L300-L320

Check the typescript file

patriksharma commented 3 years ago

@aboveyunhai @enestatli Please see explained version of the issue. Let me post everything so you can understand what is the problem or issue here.

First I have declared a scopes:


  const options = {
      scopes: [
        Scopes.FITNESS_ACTIVITY_READ,
        Scopes.FITNESS_ACTIVITY_WRITE,
        Scopes.FITNESS_BODY_READ,
        Scopes.FITNESS_BODY_WRITE,
        Scopes.FITNESS_SLEEP_READ,

      ],
    }
  1. Then I have created a function to get sleep data
getSleepdata()
  {
    let opt = {
      startDate: "2021-04-01T00:00:17.971Z", // required
      endDate: new Date().toISOString(), // required

    };

    GoogleFit.getSleepSamples(opt).then((res)=> {
      console.log("sleepdata",res)
    });
  }

Now when I call this function I receive this as output log. You will see its not giving me data as required (date, steps etc). It simply showing be [object] Screenshot 2021-04-20 at 10 16 54 AM

Where Sleep {success:true} is value of Authorized method.

GoogleFit.authorize(options)
    .then(authResult => {
      if (authResult.success) {
        console.log("Sleep",authResult);

      } else {
        console.log("Sleep",authResult.message);
        //dispatch("AUTH_DENIED", authResult.message);
      }
    })
    .catch(() => {

    })
aboveyunhai commented 3 years ago

@patriksharma This is the expected behavior if your log does not handle nested data structures. Object here refers to the type of content instead of the content itself. You need to iterate through the array to check each element by yourself. When in doubt, try some codes out.

It seems like you don't understand some JavaScript stuffs, and heavily rely on log() info. so I highly suggest you search your problem or ask it in StackOverflow first. This question technically has nothing to do with this package.

To give you an idea: https://stackoverflow.com/questions/10729276/how-can-i-get-the-full-object-in-node-jss-console-log-rather-than-object

patriksharma commented 3 years ago

@aboveyunhai Thanks a lot for sharing that link, It looks helpful. Yes I'm new to react native and worked only on Swift.

thanks for helping out here.

enestatli commented 3 years ago

I think we both understand the issue but the point you missing is the logged array holds the all sleep data starting from startDate: "2021-04-01T00:00:17.971Z" until now. So you need to filter thorugh that array and check if data.source === 'com.whatyouneed_steps' and map to the filtered list to get the sleep,steps or whatever you need values.

patriksharma commented 3 years ago

console.log(JSON.stringify(myObject, null, 4));

yes worked with this

 [
    {
        "source": "com.google.android.gms:estimated_steps",
        "steps": [
            {
                "date": "2021-04-15",
                "value": 27
            },
            {
                "date": "2021-04-16",
                "value": 3720
            },
            {
                "date": "2021-04-17",
                "value": 10415
            },
            {
                "date": "2021-04-19",
                "value": 4204
            },
            {
                "date": "2021-04-20",
                "value": 473
            },
            {
                "date": "2021-04-21",
                "value": 118
            }
        ],