alvarowolfx / asset-tracker-gcp-mongoose-os

🚧 An Asset Tracker made with an ESP32 running MongooseOS + GPS and GPRS Module, sending data through Google Cloud IoT Core
https://medium.com/google-cloud/gps-cellular-asset-tracking-using-google-cloud-iot-core-firestore-and-mongooseos-4dd74921f582
71 stars 22 forks source link

Function getCloudIoTClient() crashes at 'getApplicationDefault' #6

Open loicdrbx opened 5 years ago

loicdrbx commented 5 years ago

I am getting the following error from the Firebase logs once the getCloudIoTClient() part my function runs:

typeerror: Cannot read property 'getApplicationDefault' of undefined at Promise (/srv/index.js:98:21) at new Promise () at getCloudIoTClient (/srv/index.js:97:10) at exports.updateConfig.functions.database.ref.onUpdate (/srv/index.js:58:5) at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:109:23) at /worker/worker.js:726:24 at at process._tickDomainCallback (internal/process/next_tick.js:228:7)

Any insight as to why this may be happening? I have customized the updateDeviceConfig function to work with the RealTime Database and my own data set, but I don't see why that would the source of the problem.

/** Update device configuration when data is written to firebase */
exports.updateDeviceConfig = functions.database
  .ref('/devices/{deviceId}/')
  .onUpdate((change, context) => {

    const deviceId = context.params.deviceId;
    const device_name = `projects/${PROJECT_ID}/locations/${REGION}/registries/${REGISTRY}/devices/${deviceId}`;

    const newConfig = change.after.val(); 
    const data = new Buffer(JSON.stringify(newConfig)); 
    const binaryData = data.toString('base64');

    const request = {
      name: device_name,
      resource: {
        version_to_update: 0,
        data: {
          binary_data: binaryData
        }
      }
    };

    console.log(newConfig);
    console.log(request);

    getCloudIoTClient().then(client => {
      client.projects.locations.registries.devices.modifyCloudToDeviceConfig(request,
        (err, data) => {
          if (err) {
            console.log('Could not update config:', deviceId);
            console.log('Message: ', err);
          } else {
            console.log('Success :', data);
          }
        });
        return;
    })
    .catch(err => {
      console.log(err);
    });

    return true;
  });

// Constants needed for getIoTClient method
const API_SCOPES = ['https://www.googleapis.com/auth/cloud-platform'];
const API_VERSION = 'v1';
const DISCOVERY_API = 'https://cloudiot.googleapis.com/$discovery/rest';
const SERVICE_NAME = 'cloudiot';
const DISCOVERY_URL = `${DISCOVERY_API}?version=${API_VERSION}`;

function getCloudIoTClient() {
  return new Promise((resolve, reject) => {
    googleapis.auth.getApplicationDefault((err, auth, projectId) => {
      if (err) {
        reject(err);
        return;
      }

      googleapis.discoverAPI(DISCOVERY_URL, { auth }, (err, service) => {
        if (!err) {
          resolve(service);
        } else {
          reject(err);
        }
      });
    });
  });
}