skip to Main Content

My function pulls from 2 firestore Collections: Media, and Users. Inside Users is a subcollection with a list of all of their movies. Media contains the movies’ details. I want my function to grab all movies in their ‘MediaList’, access the media_uid field inside of the document, and use this to access all Media with matching media_uid.

What I have tried:

  1. verified that i imported FieldPath correctly:
import { getDocs, collection, getDoc, setDoc, addDoc, updateDoc, doc, where, query, writeBatch, FieldPath } from "firebase/firestore"
  1. verified that all media_uid’s are also in the Media Collection

here is the specific code which is causing the error:

const mediaQuery = query(mediaRef, where(FieldPath.documentId(), 'in', media_ids));

and here is the full function:

export const getUserMedia = async (uid) => {
    try {
        const userDocRef = doc(db, 'Users', uid);
        const mediaListCollectionRef = collection(userDocRef, 'MediaList');
        const mediaListSnapshot = await getDocs(mediaListCollectionRef);
        const userData = mediaListSnapshot.docs.map((doc) => ({ ...doc.data(), key: doc.id }));

        // Get the document IDs directly from the MediaList collection
        const media_ids = mediaListSnapshot.docs.map((doc) => doc.id);
        console.log('media_ids:', media_ids);

        const mediaRef = collection(db, 'Media');

        // Use where-in query to fetch only documents with specified document IDs
        const mediaQuery = query(mediaRef, where(FieldPath.documentId(), 'in', media_ids));
        const mediaSnapshots = await getDocs(mediaQuery);
        console.log('mediaSnapshots:', mediaSnapshots.docs);

        const combinedData = mediaSnapshots.docs.map((docSnapshot, index) => {
            const mediaData = docSnapshot.exists() ? docSnapshot.data() : null;
            return mediaData ? { ...mediaData, ...userData[index] } : null;
        });

        console.log('combinedData:', combinedData);

        // return combinedData.filter((data) => data !== null);
    } catch (err) {
        console.error(err);
    }
};

resulting error:

TypeError: firebase_firestore__WEBPACK_IMPORTED_MODULE_0__.FieldPath.documentId is not a function

2

Answers


  1. I see you’re using webpack. Are you building for frontend client?

    According to the docs, there’s no documentId method in FieldPath.
    https://firebase.google.com/docs/reference/js/firestore_.fieldpath

    You might be looking at the server library reference, which is not suitable to use on the frontend.

    Login or Signup to reply.
  2. To refer to the ID of a document, you should use the documentId method from the firebase modular sdk.

    import { documentId } from 'firebase/firestore'
    
    const mediaQuery = query(mediaRef, where(documentId(), 'in', media_ids));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search