Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Can't figure out what's casing this problem

mario

New Coder
I have a form I am using to allow users to add comments to my site. The form has an input field, a textarea field, and a button. When the button is clicked it runs my addComment() function which adds the name, comment, and timestamp to my firestore collection as a new doc.

It seems like after I click the button to add a comment I have to wait a few seconds before I can post another one. If I try to add a new comment too quickly then the request doesn't get sent to my firestore collection, but if I wait a few seconds everything works as expected.

I am curious if this is normal behavior? How can I set it up so users can always post comments without having to wait a few seconds? Can someone explain to me what is happening?

JavaScript:
import React, { useState, useEffect } from "react";
import { NavBar, Footer, Home, About } from "./imports";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { db } from "./firebase-config";
import {
  collection,
  getDocs,
  doc,
  updateDoc,
  addDoc,
  Timestamp,
} from "firebase/firestore";

export default function App() {
  const [formData, setFormdata] = useState([]);
  const [numberOfVisitors, setnumberOfVistors] = useState([]);
  const [userComments, setUserComments] = useState([]);

  const portfolioStatsRef = collection(db, "portfolio-stats");
  const userCommentsRef = collection(db, "user-comments");

  let sessionKey = sessionStorage.getItem("sessionKey");

  const addComment = async () => {
    const newComment = {
      name: formData.name,
      comment: formData.comment,
      date: Timestamp.now(),
    };

    try {
      await addDoc(userCommentsRef, newComment);
    } catch (err) {
      console.log(err);
    }
  };

  const handleFormData = (event) => {
    setFormdata((prevFormData) => {
      return {
        ...prevFormData,
        [event.target.name]: event.target.value,
      };
    });
  };

  useEffect(() => {
    const getVisitorCount = async () => {
      try {
        const dataFromPortfolioStatsCollection = await getDocs(
          portfolioStatsRef
        );

        setnumberOfVistors(
          dataFromPortfolioStatsCollection.docs.map((doc) => {
            return { ...doc.data(), id: doc.id };
          })
        );
      } catch (err) {
        console.log(err + " at getVisitorCount function");
      }
    };
    getVisitorCount();
  });

  useEffect(() => {
    const updateVisitorCount = async () => {
      try {
        console.log("running at updatevistor func");
        const portfolioStatsDoc = doc(
          db,
          "portfolio-stats",
          numberOfVisitors[0].id
        );
        const updatedFields = {
          visitor_count: numberOfVisitors[0].visitor_count + 1,
        };
        await updateDoc(portfolioStatsDoc, updatedFields);
      } catch (err) {
        console.log(err + " at updateVisitorCount function");
      }
    };

    if (!numberOfVisitors.length) return;

    if (sessionKey === null) {
      sessionStorage.setItem("sessionKey", "randomString");
      updateVisitorCount();
    }
  }, [numberOfVisitors]);

  useEffect(() => {
    const getUserComments = async () => {
      try {
        const dataFromUserCommentsCollection = await getDocs(userCommentsRef);

        setUserComments(
          dataFromUserCommentsCollection.docs.map((doc) => {
            return { ...doc.data(), id: doc.id };
          })
        );
      } catch (err) {
        console.log(err + " at getUserComments function");
      }
    };
    getUserComments();
  }, [userCommentsRef]);

  const currentNumberOfVisitors = numberOfVisitors.map((visitors) => {
    return (
      <h2 className="p-3 mb-0 bg-dark bg-gradient text-white" key={visitors.id}>
        Number of vistors: {visitors.visitor_count}
      </h2>
    );
  });

  const listOfUserComments = userComments.map((comment) => {
    return (
      <li className="list-group-item" key={comment.id}>
        <div className="d-flex w-100 justify-content-center">
          <h5 className="mb-1">{comment.name}</h5>
          <small>{comment.date.toDate().toString()}</small>
        </div>
        <p className="d-flex justify-content-center mb-1">{comment.comment}</p>
      </li>
    );
  });

  return (
    <>
      <div className="d-flex flex-column overflow-hidden min-vh-100 vh-100">
        <NavBar />
        <div className="row">
          <div className="col text-center">
            {numberOfVisitors.length === 0 && (
              <h2 className="p-3 mb-0 bg-dark bg-gradient text-danger">
                Sorry, the Firestore free tier quota has been met for today.
                Please come back tomorrow to see portfilio stats.
              </h2>
            )}
            {currentNumberOfVisitors}
          </div>
        </div>
        <div className="bg-image">
          <div className="postion-relative">
            <main className="flex-grow-1">
              <div className="container-fluid p-0">
                <Router>
                  <Routes>
                    <Route path="/" element={<Home />} />
                    <Route path="/about" element={<About />} />
                  </Routes>
                  <div className="row">
                    <div className="center-items col">
                      <h4 className="">Comments</h4>
                    </div>
                  </div>
                  <div className="row">
                    <div className="center-items col">
                      <div className="comments-container">
                        {userComments.length === 0 && (
                          <h4 className="text-danger bg-dark m-1 p-1">
                            Sorry, the Firestore free tier quota has been met
                            for today. Please come back tomorrow to see
                            portfilio comments.
                          </h4>
                        )}
                        {listOfUserComments}
                      </div>
                    </div>
                  </div>
                  <div className="row">
                    <div className="center-items col">
                      <h4 className="text-dark">Leave a comment</h4>
                    </div>
                  </div>
                  <div className="row">
                    <div className="center-items col">
                      <form className="comment-form">
                        <div className="form-floating mb-3">
                          <input
                            type="text"
                            className="bg-transparent form-control"
                            id="floatingInput"
                            name="name"
                            onChange={handleFormData}
                          />
                          <label htmlFor="floatingInput">Name</label>
                        </div>
                        <div className="form-floating">
                          <textarea
                            className="form-textarea-field bg-transparent form-control mb-1"
                            name="comment"
                            id="floatingTextarea"
                            onChange={handleFormData}
                          />
                          <label htmlFor="floatingTextarea">Comment</label>
                        </div>
                        <div className="d-grid">
                          <button
                            className="btn btn-primary mb-4"
                            onClick={addComment}
                          >
                            Add Comment
                          </button>
                        </div>
                      </form>
                    </div>
                  </div>
                </Router>
              </div>
            </main>
          </div>
        </div>
        <Footer />
      </div>
    </>
  );
}
 
Is it possible you're being rate-limited by Firebase and you're only allowed to write once per second or so?
I hadn't considered that. According to the official documentation here the maximum writes per second per database is 10,000 (up to 10 MiB per second). How can I check how usage one write to my database uses?
 
Are you sure you're not hitting the one per second limit listed below that section:

Maximum sustained write rate to a document - 1 per second

I'm not familiar with Firebase so I'm not sure how to check if you're hitting that limit, but if you only have this problem when saving and then immediately trying to save again, it seems like a likely cause of your issue.
 
Are you sure you're not hitting the one per second limit listed below that section:



I'm not familiar with Firebase so I'm not sure how to check if you're hitting that limit, but if you only have this problem when saving and then immediately trying to save again, it seems like a likely cause of your issue.
I figured out the problem I had to use preventDefault so that my form wouldn't refresh the page before the comment could be added to the db. Thank you for all your help.
 

New Threads

Buy us a coffee!

Back
Top Bottom