【问题标题】:FirebaseError: Invalid document reference. Document references must have an even number of segments,FirebaseError:无效的文档参考。文档引用必须有偶数个段,
【发布时间】:2021-12-06 13:01:09
【问题描述】:

第一次寻求帮助。

在这个问题上卡了一段时间。

// 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)
    }
}

我得到的错误是: FirebaseError:无效的文档参考。文档引用必须有偶数个段,但 udstyr/hRjRFVcgr8Q2nCeHcNbp/8zTbs2qnPOMWW91JTksb 有 3 个。

尝试将方法更改为以下:

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

现在我收到一个我看不到的错误。老实说,不知道错误是什么。

我上面有这个功能,就像一个魅力:

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

所以这不是收集错误的情况。

编辑:由于这里的数据不敏感,因此是整个控制器以及 firestore 数据库的外观。

Firestore 数据库:

而具有 3 个工作的控制器得到 + 我的创建不起作用。

// 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)
    }
}

【问题讨论】:

  • opretUdstyr 的参数之一可能为空。通过添加一个格式明确的文字对象来排除这种情况,例如:await addDoc(collection(db, 'udstyr'), { testKey: 'testValue' });
  • 我在另一张纸上有一个功能,可以事先检查,如果 3 个外部参数中的任何一个为空,它就不会进入控制器。但否则是一个有效的问题。

标签: javascript firebase google-cloud-firestore


【解决方案1】:

Firestore 数据模型是:

  1. 在顶层您有收藏。
  2. 在每个集合中,您(可以)有文档。
  3. 每个文档都可以再次嵌套集合。

所以文档的路径总是thecollection/thedocidthecollection/thedocid/nestedcollection/anotherdocid 等。

在您的第一个代码 sn-p 中:

const docref = doc(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);

【讨论】:

  • 尝试了您的子集合,但遗憾的是它没有工作。我也尝试过您的其他解决方案。所有选项都带有与我自己的第二次尝试相同的错误。即使使用 try catch 来显示错误,我也无法捕捉到。如果可能的话,您是否会对一对一的不和谐对话或类似的对话感兴趣。
  • 用更多信息更新了我原来的帖子。
  • 如果我读到您的更新是正确的,那么您在ID 字段中使用UUID 来表示GetDocsUUID 不是文档的 ID,文档的 ID 是您给它的名称。 Firebase 无法将“UUID”识别为 ID。有关GetDoc 调用的示例,请参阅here
  • @Selena ,抱歉,忘记@你了更明确...尽管他们一直称文档的名称为“ID”
  • @fabc 感谢您的帮助。我希望我能帮助未来的人,如果他们以同样的方式发生我的错误,为什么会发生我的错误。但可惜我很高兴它有效。
猜你喜欢
  • 1970-01-01
  • 2019-04-17
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 2019-05-24
相关资源
最近更新 更多