title: Adding documents to firestore using javascript
body: <p>I'm trying to create a function to add documents to firestore. At the moment this function could be called two different ways. 1) Adding a user to a collection called 'Users'. 2) adding a site to a collection called 'Sites'.</p>
  <p>This function should take the following parameters:</p>
  <ol>
  <li><p>The name of the collection (required)</p>
  </li>
  <li><p>The users uid (make this optional)</p>
  </li>
  <li><p>An object with the data for a site (make this optional)</p>
  </li>
  </ol>
  <p>I'm also trying to utilize JS modules to keep my code better organised. At the moment my folder looks something like this:</p>
  <pre><code>myApp
    assets
      img
    dist
      index.html
      index.js
    modules
      auth.js
      firestore.js
  </code></pre>
  <p>Inside auth.js I have a signUp() inside witch I want to call a function called addToFirestore() <strong>(this comes from firestore.js)</strong>. My code looks something like this:</p>
  <p><strong>firestore.js</strong></p>
  <pre><code>import { initializeApp } from 'https://www.gstatic.com/firebasejs/9.6.4/firebase- 
  app.js';
  
  import { getFirestore, setDoc, doc } from 
  'https://www.gstatic.com/firebasejs/9.6.4/firebase-firestore.js';
  
  const firebaseConfig = {
    ...
  };
  
  // Initialize Firebase
  initializeApp(firebaseConfig);
  const db = getFirestore;
  
  function addToFirestore(collName, user = 0, data = 0) {
    // check if adding user
    if (user != 0 &amp;&amp; data == 0){
      //adding user to firestore
      try {
        setDoc(doc(db, collName, user.uid), {
          email: user.email,
        });
      } catch (e) {
        console.error('Error adding document: ', e);
      }
    // check if adding site
  } else if (data != 0 &amp;&amp; user == 0) {
    setDoc(doc(db, collName), data);
  }
  
  export { addToFirestore};
  </code></pre>
  <p>Inside <strong>auth.js</strong> calling function like this:</p>
  <pre><code>// * Auth
  import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword } from 
  'https://www.gstatic.com/firebasejs/9.6.4/firebase-auth.js';
  
  import {addToFirestore} from '/modules/firestore.js';
  
  function signUp(email, password) {
    createUserWithEmailAndPassword(auth, email, password)
      .then((userCredential) =&gt; {
        const user = userCredential.user;
        addToFirestore('Users', user);
      })
      .then(() =&gt; {
        openApp();
      })
      .catch((error) =&gt; {
        const errorCode = error.code;
        const errorMessage = error.message;
        alertAuth.classList.remove('d-none');
        alertAuth.classList.add('show');
        alertAuth.innerHTML = `&lt;strong&gt;Error: &lt;/strong&gt; ${errorCode}`;
      });
  }
  </code></pre>
  <p>And inside <strong>index.js</strong> calling signUp():</p>
  <pre><code>btnAuthSignUp.addEventListener('click', function () {
    event.preventDefault();
    let email = inpAuthEmail.value;
    let password = inpAuthPassword.value;
  
    signUp(email, password);
  });
  </code></pre>
  <p>And it is giving me an error like this:</p>
  <blockquote>
  <p>firestore.js:31 Error adding document:  FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore</p>
  </blockquote>
  <p>I tried doing everything in one file and still got the same problem. Any help would be greatly appreciated.</p>