Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FirebaseError: Invalid document reference. Document references must have an even number of segments,

First time asking for help.

Been stuck on this issue for a while.

// Database setup
import { initializeApp } from "firebase/app";
import { doc, setDoc, addDoc, collection, getFirestore, getDocs } from "firebase/firestore";
// Your web app's Firebase configuration

// Your web app's Firebase configuration
const firebaseConfig = {the data in here}

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

/**
 * Creates new eqipment
 */
export async function opretUdstyr(navn, type, note) {
    try {
        const uuid = generateUUID();
        let nyUdstyr = {
            navn: navn,
            type: type,
            note: note,
            status: "Tilgængelig",
            fra: "",
            til: "",
            UUID: uuid
        };
        const docref = doc(collection(db, 'udstyr'));
        await addDoc(docref, nyUdstyr);
    } catch (e) {
        console.error(e)
    }
}

The error I get is: FirebaseError: Invalid document reference. Document references must have an even number of segments, but udstyr/hRjRFVcgr8Q2nCeHcNbp/8zTbs2qnPOMWW91JTksb has 3.

tried changing the method to the following:

const docref = doc(collection(db, 'udstyr'));
to
const docref = collection(db, 'udstyr');

now I get an error I can't see. Honestly have no idea what the errors is.

I have this function above that works like a charm:

export async function getUdstyr() {
    const equipment = collection(db, 'udstyr');
    const snapshot = await getDocs(equipment);
    const result = snapshot.docs.map(doc => doc.data());
    return result;
}

So its not a case of wrong collection.

EDIT: Since the data arent sensitive here is the entire controller as well as the look of the firestore database.

Firestore database: enter image description here

And the controller with the 3 working gets + my create that don't work.

// Database setup
import { initializeApp } from "firebase/app";
import { doc, setDoc, addDoc, collection, getFirestore, getDocs } from "firebase/firestore";
// Your web app's Firebase configuration

// Your web app's Firebase configuration
const firebaseConfig = {
    apiKey: "AIzaSyAOW2ufaAdHaOvP82u5VwoNVZ9bgkzWh-E",
    authDomain: "booking-system-test-866d0.firebaseapp.com",
    projectId: "booking-system-test-866d0",
    storageBucket: "booking-system-test-866d0.appspot.com",
    messagingSenderId: "413437808749",
    appId: "1:413437808749:web:610bfaabb471329bbeef64"
};

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

/**
 * Returns snapshot of all equipment
 */
export async function getUdstyr() {
    const equipment = collection(db, 'udstyr');
    const snapshot = await getDocs(equipment);
    const result = snapshot.docs.map(doc => doc.data());
    return result;
}

/**
 * Returns snapshot of all bookings
 */
export async function getBookings() {
    const booking = collection(db, 'bookings');
    const snapshot = await getDocs(booking);
    const result = snapshot.docs.map(doc => doc.data());
    return result;
}

/**
 * Returns snapshot of all rentings
 */
export async function getUdlaan() {
    const udlaan = collection(db, 'udlaan');
    const snapshot = await getDocs(udlaan);
    const result = snapshot.docs.map(doc => doc.data());
    return result;
}

/**
 * Creates new eqipment
 */
export async function opretUdstyr(navn, type, note) {
    try {
        const uuid = generateUUID();
        let nyUdstyr = {
            navn: navn,
            type: type,
            note: note,
            status: "Tilgængelig",
            fra: "",
            til: "",
            UUID: uuid
        };
        /*const docref = collection(db, 'udstyr');
        await addDoc(docref, nyUdstyr);*/

        await addDoc(collection(db, 'udstyr'), nyUdstyr);

        /*const docref = doc(collection(db, 'udstyr'));
        const colref = collection(docref, 'subcollection');
        await addDoc(colref, nyUdstyr);*/

    } catch (e) {
        console.error(e)
    }
}
like image 968
Selena Avatar asked Oct 18 '25 11:10

Selena


1 Answers

The Firestore data model is:

  1. At the top level you have collections.
  2. In each collection you (can) have documents.
  3. Each each document you can have nested collections again.
  4. Etc.

So a path to a document is always going go be thecollection/thedocid or thecollection/thedocid/nestedcollection/anotherdocid, etc.

In your first code snippet you do:

const docref = doc(collection(db, 'udstyr'));
await addDoc(docref, nyUdstyr);

The first line here sets up a reference to a document. But the second line then tries to add a document to that document, which isn't possible according to the model I gave.

A document can only exist in a (sub)collection, not directly inside another document. So you can either add it to the top-level collection with:

await addDoc(collection(db, 'udstyr'), nyUdstyr);

Or if you want, you can add the document to a subcollection with something like:

const docref = doc(collection(db, 'udstyr'));
const colref = collection(docref, 'subcollection'));
await addDoc(colref, nyUdstyr);
like image 134
Frank van Puffelen Avatar answered Oct 19 '25 23:10

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!