title: How do I use Firebase Admin SDK to change data in Firebase Realtime Database while using Cloud Functions?
body: <p>I want to reset a specific value in my Firebase Realtime Database every day at 12:00 AM. To do this, I'm using the Firebase Admin SDK to change the data in Firebase Realtime Database and Cloud Functions to trigger the change at 12:00 AM every day.</p>
  <p>This is an example structure of my Firebase Realtime Database:</p>
  <pre><code>{
    &quot;users&quot;: {
      &quot;fa54487d9cbb4214b00db80e2118e4e6&quot;: {
        &quot;daily&quot;: 10
      }
    }
  }
  </code></pre>
  <p>This is the code in my <code>index.js</code>:</p>
  <pre><code>// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
  var functions = require('firebase-functions');
  
  // The Firebase Admin SDK to access Cloud Firestore.
  var admin = require('firebase-admin');
  
  // Fetch the service account key JSON file contents
  var serviceAccount = require(&quot;serviceAccountKey.json&quot;);
  
  // Initialize the app with a service account, granting admin privileges
  admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: &quot;https://databaseName.firebaseio.com&quot;
  });
  
  // As an admin, the app has access to read and write all data, regardless of Security Rules
  var db = admin.database();
  var ref = db.ref(&quot;users&quot;);
  
  // Reset today GHG emissions at 12:00 AM everyday
  exports.dailyReset = functions.pubsub.schedule('0 0 * * *').onRun((context) =&gt; {
      usersRef.child(&quot;{userId}&quot;).set({
        daily: 0
      });
  });
  </code></pre>
  <p>Deploy Error:</p>
  <blockquote>
  <p>!  functions[dailyReset(us-central1)]: Deployment error.</p>
  <p>Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: <a href="https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs</a>. Additional troubleshooting documentation can be found at <a href="https://cloud.google.com/functions/docs/troubleshooting#logging" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/troubleshooting#logging</a>. Please visit <a href="https://cloud.google.com/functions/docs/troubleshooting" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/troubleshooting</a> for in-depth troubleshooting documentation.</p>
  </blockquote>
  <p>Firebase Console Functions Logs:</p>
  <blockquote>
  <p>Error: function terminated. Recommended action: inspect logs for termination reason.</p>
  <p>Additional troubleshooting documentation can be found at <a href="https://cloud.google.com/functions/docs/troubleshooting#logging" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/troubleshooting#logging</a> Function cannot be initialized.</p>
  <p>{&quot;@type&quot;:&quot;type.googleapis.com/google.cloud.audit.AuditLog&quot;,&quot;status&quot;:{&quot;code&quot;:3,&quot;message&quot;:&quot;Function failed on loading user code. This is likely due to a bug in the user code.</p>
  <p>Error message: Error: please examine your function logs to see the error cause: <a href="https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs</a>.</p>
  <p>Additional troubleshooting documentation can be found at <a href="https://cloud.google.com/functions/docs/troubleshooting#logging" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/troubleshooting#logging</a>.</p>
  </blockquote>
  <p>The script won't deploy when I use <code>firebase deploy</code> as my function is giving me an error. Can someone tell me how to fix my code?</p>