title: Firebase firestore Cannot read property 'firebase' of undefined
body: <p>I'm trying to create a collection called &quot;users&quot; but there seems to be a problem with the firebase reference , I'm using an already existing repo where I added my addUserToDb() function, in the function above called saveMessagingDeviceToken() there seems to be no problem with the firebase.</p>
  <pre><code>   &lt;code&gt;
  // Saves the messaging device token to the datastore.
  function saveMessagingDeviceToken() {
    firebase.messaging().getToken().then(function(currentToken) {
      if (currentToken) {
        console.log('Got FCM device token:', currentToken);
        // Saving the Device Token to the datastore.
        firebase.firestore().collection('fcmTokens').doc(currentToken)
            .set({uid: firebase.auth().currentUser.uid});
      } else {
        // Need to request permissions to show notifications.
        requestNotificationsPermissions();
      }
    }).catch(function(error){
      console.error('Unable to get messaging token.', error);
    });
  }
  
      // Add the user to DB
      function addUserToDb(profilePicUrl, userName, userID){
        var db = this.firebase.firestore();
        db.collection(&quot;users&quot;).doc(userID).set({
          name: userName,
          profilePic: profilePicUrl,
          uid: userID
      })
      .then(function() {
          console.log(&quot;Document successfully written!&quot;);
      })
      .catch(function(error) {
          console.error(&quot;Error writing document: &quot;, error);
      });
      
      }
  
  &lt;/code&gt;
  </code></pre>
  <p>The way to add is taken from <a href="https://firebase.google.com/docs/firestore/manage-data/add-data" rel="nofollow noreferrer">https://firebase.google.com/docs/firestore/manage-data/add-data</a> which is the documentation.</p>
  <p>the full error :</p>
  <pre><code>TypeError: Cannot read property 'firebase' of undefined
      at addUserToDb (main.js:115)
      at Object.authStateObserver [as next] (main.js:199)
      at subscribe.ts:104
      at subscribe.ts:233
  </code></pre>