skip to Main Content

I can’t write to firestore. It’s working for Storage and Realtime Database, but not for firestore, can you suggest where the issue might be?

firebase.js

import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
  ...
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
export const firestore = getFirestore(app);

CheckoutForm.js

import { collection, addDoc } from "firebase/firestore"; 
import { firestore } from "../../firebase";

export const CheckoutForm = async (uid) => {
  console.log("object 1")
  try {
    const docRef = await addDoc(collection(firestore, "documents"), {
      first: "Ada",
      last: "Lovelace",
      born: 1815
    });
    console.log("Document written with ID: ", docRef.id);
  } catch (e) {
    console.error("Error adding document: ", e);
  }
  console.log("object 2")
}

Here I call CheckoutForm.
Home.js

import { loadStripe } from "@stripe/stripe-js";
import { Elements } from "@stripe/react-stripe-js"

const PUBLIC_KEY = "pkey_test_stripe"

const stripeTestPromise = loadStripe(PUBLIC_KEY);

export default function Home() {
return (
  <Elements stripe={stripeTestPromise}>
   <div> 
    <button onClick={() => CheckoutForm(currUser.uid)}>
       Upgrade to premium!
    </button>
   </div>
  </Elements>
);
}

For some reason I don’t get any response or error. Console written "object1", but after try/catch construction nothing runs.

2

Answers


  1. Chosen as BEST ANSWER

    I have found the root of problem, there was title directly in firestore "Databases linking, you should unlink if you want use this", some king of that. So I have clicked and after all works.


  2. export const CheckoutForm = async (uid) => {
        console.log("object 1")
        try {
          const docRef = await addDoc(collection(firestore, "documents"), {
            first: "Ada",
            last: "Lovelace",
            born: 1815
          });
          console.log("Document written with ID: ", docRef.id);
        } catch (e) {
          console.error("Error adding document: ", e);
        }
        console.log("object 2")
      }
    await CheckoutForm();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search