Home > Back-end >  How do i solve this error: FirebaseError: Expected type 'mc', but it was: a custom yc obje
How do i solve this error: FirebaseError: Expected type 'mc', but it was: a custom yc obje

Time:05-05

I created a app on Nextjs (js) and Firebase. And I added Stripe extension to make a subscription system. I'm not very experienced with this new version 9 of firebase so it was hard to do this because I followed a tutorial that using version 8 and ts. I still managed to make most of the things work except this that is giving me this error.

This is the tutorial: https://youtu.be/P0Udy2Gi7n8?t=859 (my problem at 14:19)

I'm having the error here:

import getStripe from "./initializeStripe";
import { db } from '../firebase/firebaseClient'
import { doc, getDoc, addDoc, setDoc, setCol, onSnapshot, collection } from "firebase/firestore";

export async function createCheckoutSession(uid) {

    const docData = {
        price: "price_1KvNgoI6qyGEmrlgKU3miVmT",
        success_url: window.location.origin,
        cancel_url: window.location.origin,
    };
  
    const checkoutSessionRef = await setDoc(collection(db, "users", uid, "checkout_sessions"), docData); //ERROR


    onSnapshot(getDoc(doc(checkoutSessionRef)), (snap) => {
        const { sessionId } = snap.data();

        if (sessionId) {
            // We have a session, let's redirect to checkout
            // Init Stripe
            const stripe = getStripe();
            stripe.redirectToCheckout({ sessionId })
        }
    });

The error is the following:

Unhandled Runtime Error FirebaseError: Expected type 'mc', but it was: a custom yc object

Browser

Firebase config

CodePudding user response:

The setDoc() takes a DocumentReference as first parameter but you are passing a CollectionReference. Try using addDoc() instead:

await addDoc(collection(db, "users", uid, "checkout_sessions"), {...data})

This will add a document with a randomly generated document ID. If you want to use specify the document yourself, then you can use setDoc() as shown below:

await setDoc(doc(db, "users", uid, "checkout_sessions", "DOC_ID"), {...data})
  • Related