最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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:

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",
    projectId: "booking-system-test-866d0",
    storageBucket: "booking-system-test-866d0.appspot",
    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)
    }
}

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:

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.",
    projectId: "booking-system-test-866d0",
    storageBucket: "booking-system-test-866d0.appspot.",
    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)
    }
}
Share Improve this question edited Dec 6, 2021 at 17:18 Selena asked Dec 6, 2021 at 13:01 SelenaSelena 551 gold badge1 silver badge6 bronze badges 2
  • Maybe one of the params to opretUdstyr is null. Rule that out by adding a literal object that is clearly well-formed, like: await addDoc(collection(db, 'udstyr'), { testKey: 'testValue' }); – danh Commented Dec 6, 2021 at 17:28
  • I have a function on another sheet that checks that beforehand, it won't get into the controller if any of the 3 outside parameters is null. But otherwise a valid problem. – Selena Commented Dec 6, 2021 at 17:36
Add a ment  | 

1 Answer 1

Reset to default 13

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);
发布评论

评论列表(0)

  1. 暂无评论