title: How to fix firebase database initialised multiple times due to React SSR initialised database and cloud function firebase initialised database?
body: <p>I have updated the question as found the root cause of the issue.
  As I have hosted my React SSR app which uses firebase database in the client serving by one of the cloud function named <code>app</code> throwing an error of <code>Error: FIREBASE FATAL ERROR: Database initialized multiple times. Please make sure the format of the database URL matches with each database() call.</code>. When I comment out one by one and deploy, works perfectly. But when I deploy together doesn't work. How do I separate these two keeping both at the same repo?</p>
  
  <p>ORIGINAL Question: <code>Why firebase cloud function throwing an error of 'The default Firebase app does not exist.'?</code>
  So I am trying out firebase function for the first time. admin.messaging() throwing me the following error. Help me figure out why?
  If I look at the console I get results till <code>console.log('deviceToken', deviceToken);</code></p>
  
  <p>so whats wrong in <code>const messageDone = await admin.messaging().sendToDevice(deviceToken, payload);</code>?</p>
  
  <pre><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  exports.updateUnreadCount = functions.database.ref('/chats/{chatId}/{messageId}')
    .onCreate(async(snap, context) =&gt; {
      const appOptions = JSON.parse(process.env.FIREBASE_CONFIG);
      appOptions.databaseAuthVariableOverride = context.auth;
      const adminApp = admin.initializeApp(appOptions, 'app');
      const { message, senderId, receiverUid } = snap.val();
      console.log(message, senderId, receiverUid);
      console.log('------------------------');
      const deleteApp = () =&gt; adminApp.delete().catch(() =&gt; null);
      try {
        const db = adminApp.database();
        const reciverUserRef = await db.ref(`users/${receiverUid}/contacts/${senderId}/`);
        console.log('reciverUserRef', reciverUserRef);
        const deviceTokenSnapshot = await reciverUserRef.child('deviceToken').once('value');
        const deviceToken = await deviceTokenSnapshot.val();
        console.log('deviceToken', deviceToken);
  
        const payload = {
          notification: {
            title: 'Test Notification Title',
            body: message,
            sound: 'default',
            badge: '1'
          }
        };
        const messageDone = await admin.messaging().sendToDevice(deviceToken, payload);
        console.log('Successfully sent message: ', JSON.stringify(messageDone));
        return deleteApp().then(() =&gt; res);
      } catch (err) {
        console.log('error', err);
        return deleteApp().then(() =&gt; Promise.reject(err));
      }
    });
  </code></pre>
  
  <p>Update1: According to this <a href="https://firebase.google.com/docs/cloud-messaging/send-message#send_to_a_topic" rel="nofollow noreferrer">https://firebase.google.com/docs/cloud-messaging/send-message#send_to_a_topic</a>, <code>admin.messaging().sendToDevice(deviceToken, payload)</code> APIs are only available in the Admin Node.js SDK?
  So switched to </p>
  
  <pre><code>const payload = {
          data: {
            title: 'Test Notification Title',
            body: message,
            sound: 'default',
            badge: '1'
          },
          token: deviceToken
        };
        const messageDone = await admin.messaging().send(payload);
  </code></pre>
  
  <p>Which is not working either. Getting an error <code>Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.</code> Any lead will be helpful.</p>
  
  <p>EDIT: Finally got the function working.
  My index.js is exporting to functions, follwoing</p>
  
  <pre><code>exports.app = functions.https.onRequest(app); //React SSR
  exports.updateChat = functions.database.ref('/chats/{chatId}/{messageId}').onCreate(updateChat);
  </code></pre>
  
  <p><code>exports.app</code> is a react ssr function, which I am using to host my site. This uses database too. and throwing error of <code>multiple database instance</code>.
  When I comment out one by one and deploy, works perfectly. But when I deploy together doesn't work. How do I separate these two keeping both at the same repo? Any suggestions, please?</p>