【发布时间】: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 数据库的外观。
而具有 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