Home > OS >  How can I add a sub sub collection under my previous sub collection?
How can I add a sub sub collection under my previous sub collection?

Time:10-21

i am trying to create a sub collection under the previous one that I made. The function that I currently have keeps giving me an error saying that it isn't a function. I saw a couple of similar questions on here and tried their solutions, but they haven't worked out for me. How can I fix this? Thanks.

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

// Initialize variables
const firestore = firebase.firestore();
const userDb = firestore.collection("/studiopick/studios/users");
const locationDb = firestore.collection("/studiopick/studios/locations");
const auth = firebase.auth();

//const auth = getAuth();
firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // ...
  } else {
    window.location.href = "login.html?error";
    alert("No active user please sign or sign up.");
  }
});

//Studio Services
document.getElementById("test").addEventListener("click", function () {
  //Declare Variables
  const user = firebase.auth().currentUser;

  var radios = document.getElementsByName("pointS");

  for (var i = 0; i < radios.length; i  ) {
    if (radios[i].checked) {
      // do whatever you want with the checked radio

      //Trigger Stripe Checkout

      //Enter database location
      userDb.doc(user.uid).doc("sessions").set({
        uid: user.uid,
        point: radios[i].value,
      });

      console.log("Data retrieved successfully");

      // only one radio can be logically checked, don't check the rest
      break;
    }
  }
});
@import url("https://fonts.googleapis.com/css2?family=Be Vietnam Pro:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap");

:root {
  --c-text-primary: #282a32;
  --c-text-secondary: #686b87;
  --c-text-action: #404089;
  --c-accent-primary: #434ce8;
  --c-border-primary: #eff1f6;
  --c-background-primary: #ffffff;
  --c-background-secondary: #fdfcff;
  --c-background-tertiary: #ecf3fe;
  --c-background-quaternary: #e9ecf4;
}

body {
  line-height: 1.5;
  min-height: 100vh;
  font-family: "Be Vietnam Pro", sans-serif;
  background-color: #e5e5e5 !important;
  color: var(--c-text-primary);
}

:focus {
  outline: 0;
}

.navbar-light {
  background-color: #ffffff;
}

.navbar-nav {
  justify-content: space-between;
}

.navbar-brand {
  font-size: 45px;
  color: #a388e7 !important;
  font-weight: bold;
  position: relative;
  left: -490px !important;
}

.nav-item {
  color: #686868 !important;
  font-size: 20px;
  position: relative;
  right: -440px !important;
}


.nav-item a {
  display: block;
  color: #000000;
  text-decoration: none;
  transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out,
    border-color 0.15s ease-in-out;
}

#alert {
  position: relative;
  right: -3px !important;
}

.action {
  position: relative;
  right: -450px;
}

.action .profile {
  position: relative;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
}

.action .profile img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.action .menu {
  position: absolute;
  top: 120px;
  right: -10px;
  padding: 10px 20px;
  background: #fff;
  width: 200px;
  box-sizing: 0 5px 25px rgba(0, 0, 0, 0.1);
  border-radius: 15px;
  transition: 0.5s;
  visibility: hidden;
  opacity: 0;
}

.action .menu.active {
  top: 80px;
  visibility: visible;
  opacity: 1;
}

.action .menu::before {
  content: "";
  position: absolute;
  top: -5px;
  right: 28px;
  width: 20px;
  height: 20px;
  background: #fff;
  transform: rotate(45deg);
}

*,
::after,
::before {
  box-sizing: border-box;
}

.action .menu h3 {
  width: 100%;
  text-align: center;
  font-size: 18px;
  padding: 20px 0;
  font-weight: 500;
  color: #555;
  line-height: 1.5em;
}

.action .menu h3 span {
  font-size: 14px;
  color: #cecece;
  font-weight: 300;
}

.action .menu ul li {
  list-style: none;
  padding: 16px 0;
  border-top: 1px solid rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
}

.action .menu ul li img {
  max-width: 20px;
  margin-right: 10px;
  opacity: 0.5;
  transition: 0.5s;
}

.action .menu ul li:hover img {
  opacity: 1;
}

.action .menu ul li a {
  display: inline-block;
  text-decoration: none;
  color: #555;
  font-weight: 500;
  transition: 0.5s;
}

.action .menu ul li:hover a {
  color: #a388e7;
}
.responsive-wrapper {
  width: 90%;
  max-width: 1280px;
  margin-left: auto;
  margin-right: auto;
}

/*---Dashboard---*/
.calendar{
    position: relative;
    top: 40px;
    background-color: #ffffff;
    border-radius: 15px !important;
    width: 640px;
    height: 450px;
    box-shadow: 0 3px 3px 0 rgba(0, 0, 0, 0.05), 0 5px 15px 0 rgba(0, 0, 0, 0.05);
}

/*---Current Session---*/
.session-info{
    position: relative;
    top: -410px;
    right: -650px;
    margin-top: 0;
    margin-right: 0;
    margin-bottom: 0px;
    background-color: #ffffff;
    border-radius: 15px !important;
    padding: 0px;
    width: 490px;
    height: 220px;
    box-shadow: 0 3px 3px 0 rgba(0, 0, 0, 0.05), 0 5px 15px 0 rgba(0, 0, 0, 0.05);
}
/*---Current Session---*/

/*---Upcoming Sessions---*/
.sessions{
    position: relative;
    top: -400px;
    right: -650px;
    margin-right: 0;
    margin-bottom: 0;
    margin-top: 0;
    background-color: #ffffff;
    border-radius: 15px !important;
    padding: 0px;
    width: 490px;
    height: 220px;
    box-shadow: 0 3px 3px 0 rgba(0, 0, 0, 0.05), 0 5px 15px 0 rgba(0, 0, 0, 0.05);
}

/*---Upcoming Sessions---*/

/*---Google Maps---*/
.googlemaps{
    position: relative;
    top: -390px;
    background-color: #ffffff;
    border-radius: 15px !important;
    padding: 0px;
    width: 1140px;
    height: 350px;
    box-shadow: 0 3px 3px 0 rgba(0, 0, 0, 0.05), 0 5px 15px 0 rgba(0, 0, 0, 0.05);
}

/*---Google Maps---*/


/*---Dashboard---*/
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>StudioPick</title>

    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <!---Bootstrap CSS--->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
    <link rel="stylesheet" href="CSS/studiostyle.css">

    <script>
        function menuToggle() {
            const toggleMenu = document.querySelector(".menu");
            toggleMenu.classList.toggle("active");
        }


        // SignOut
        function signOut() {
            auth.signOut();
            alert("SignOut Successfully from System");

            window.location.href = "login.html?error";
            alert("No active user please sign or sign up.");
        }
    </script>
</head>

<body>
    <!---Navbar--->
    <header>
        <nav >
            <div >
                <a  href="#"><strong>StudioPick</a>
                <button  type="button" data-bs-toggle="collapse"
                    data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent"
                    aria-expanded="false" aria-label="Toggle navigation">
                    <span ></span>
                </button>
                <div  id="navbarSupportedContent">
                    <ul >
                        <li >
                            <a  aria-curresnt="page" href="index.html">Home</a>
                        </li>
                        <li >
                            <div >
                                <div  onclick="menuToggle();">
                                    <img src="./assets/avatar.jpg" />
                                </div>
                                <div >
                                    <h3 id="profile-name"><strong>User Name</strong></h3>
                                    <p  id="userType"
                                        style="position: relative; top: -20px; right: -60px; font-size: 12px !important">
                                        Studio</p>
                                    <ul>
                                        <li>
                                            <img src="./assets/icons/user.png" /><a href="studiodash.html">Dashboard</a>
                                        </li>
                                        <li>
                                            <img src="./assets/icons/edit.png" /><a href="editprofile.html">Edit
                                                profile</a>
                                        </li>
                                        <li>
                                            <img src="./assets/icons/envelope.png" /><a href="#">Inbox</a>
                                        </li>
                                        <li>
                                            <img src="./assets/icons/settings.png" /><a href="#">Setting</a>
                                        </li>
                                        <li><img src="./assets/icons/question.png" /><a href="#">Help</a></li>
                                        <li>
                                            <img src="./assets/icons/log-out.png" /><a href="#"
                                                onclick="signOut();">Logout</a>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <!---Navbar--->
    <main >
        <div >

            <!---Calendar--->
            <div >
                <div >
                    <h5 ><strong>Studio Name</strong></h5>
                </div>





            </div>
            <!---Calendar--->
            <!---Services--->
            <div >
                <div >
                    <h5 ><strong>Services</strong></h5>

                    <label for="point">Buy point: </label>
                    <input type="radio" name="pointS" value="20"> 20
                    <input type="radio" name="pointS" value="50"> 50
                    <input type="radio" name="pointS" value="100" checked="checked">
                    100<br><br>
                    <input type="text" id="firstName">

                    <button  id="test">Test</button>

                    <h1 id="test"></h1>
                </div>
                <!---Data will be shown via Javascript--->
            </div>
            <!---Services--->
            <!---Reviews--->
            <div >
                <div >
                    <h5 ><strong>Reviews</strong></h5>
                </div>
                <!---Data will be shown via Javascript--->



            </div>
            <!---Reviews--->
            <!---Google Maps--->
            <div >
                <div >
                    <h5 ><strong>Google Maps</strong></h5>
                </div>
            </div>
            <!---Google Maps--->

        </div>
    </main>
    <!---Footer--->
    <footer >
        <p >© 2022 StudioPick.</p>
        <ul >
            <li ><a href="#">Privacy</a></li>
            <li ><a href="#">Terms</a></li>
            <li ><a href="#">Support</a></li>
        </ul>
    </footer>
    <!---Footer--->

    </div>
    </main>

    <!-- The core Firebase JS SDK is always required and must be listed first -->
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js"></script>

    <!-- TODO: Add SDKs for Firebase products that you want to use -->
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-functions.js"></script>
    <script src="https://www.gstatic.com/firebasejs/8.10.1/firebase-database.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.9.1/firebase-firestore.js"></script>


    <!---Javascript--->
    <script src="Javascript/studiopage.js"></script>
    <!---Javascript--->

    <!----More Bootstrap--->
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"
        integrity="sha384-7 zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB"
        crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
        integrity="sha384-QJHtvGhmr9XOIpI6YVutG 2QOK9T ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13"
        crossorigin="anonymous"></script>
    <!----More Bootstrap--->

</body>

</html>

CodePudding user response:

Looking at your code I understand that the error comes from these lines of code:

  userDb.doc(user.uid).doc("sessions").set({
    uid: user.uid,
    point: radios[i].value,
  });

This is normal, because userDb is a CollectionReference and userDb.doc(user.uid) is a DocumentReference which does not have any doc() property.

So if you want to create the collection sessions and add a doc you need to do as follow for example:

  const userDb = firestore.collection("/studiopick/studios/users");
  // ...

  userDb.doc(user.uid).collection("sessions").add({
    uid: user.uid,
    point: radios[i].value,
  });
  • Related