Search code examples
node.jsrestgoogle-apigoogle-fit

How to fix Error: Require at least one aggregateby? - fitness api


I keep getting these error "Error: Require at least one aggregateby" and no idea how to fix it.

I have tried many ways:

        fitness.users.dataset.aggregate({
                auth: serviceAccountAuth,
                userId: "me",
                //fields: "bucket/dataset/point/value/intVal",
                requestBody: {
                  "aggregateBy": [{
                    "dataTypeName": "com.google.step_count.delta",
                    "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
                  }],
                  "endTimeMillis": 1566733471706,
                  "startTimeMillis": 1566647071706,
                  "bucketByTime": { "durationMillis": 86400000 }
                }
    )};

and:

        fitness.users.dataset.aggregate({
                auth: serviceAccountAuth,
                userId: "me",
                //fields: "bucket/dataset/point/value/intVal",
                requestBody: {
                  aggregateBy: [{
                    dataTypeName: "com.google.step_count.delta",
                    dataSourceId: "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
                  }],
                  endTimeMillis: 1566733471706,
                  startTimeMillis: 1566647071706,
                  bucketByTime: { durationMillis: 86400000 }
                }
    )};

Anyone know how to fix it ? Any help will be really appreciated!!!


Solution

  • https://github.com/googleapis/google-api-nodejs-client/issues/1749

    this is correct:

    fitness.users.dataset.aggregate(
        {
          userId: 'me',
          resource: {
            aggregateBy: [
              {
                dataSourceId:
                  'derived:com.google.step_count.delta:com.google.android.gms:estimated_steps'
              }
            ],
            bucketByTime: {
              durationMillis: 86400000
            },
            startTimeMillis: 1584891702214,
            endTimeMillis: 1584978102214
          }
        },
        (err, res, aa) => {
          if (err) return console.log('The API returned an error: ' + err);
          console.log(res.data);
          const events = res.data.items;
          resolve(events);
        }
      );