如何将对象添加到 firebase 中的文档
how to add an object to a document in firebase
在我的代码中,我从现有的 Firebase 集合中获取了一个对象,然后我想将它作为一个对象添加到另一个文档中,但是我不知道该怎么做
// Create a query against the collection.
const q = query(subjectRef, where("Subject", "==", subject), where("Board", "==", examBoard), where("Level", "==", level));
onSnapshot(q, (snapshot) => {
snapshot.docs.forEach(doc => {
//add a document to a subcollection in the collection users
const auth = getAuth();
//editted so onAuthState function is not called
const uid = auth.currentUser.uid
const docRef = doc(db, "users", uid);
const colRef = collection(docRef, "subjects");
addDoc(colRef, {
//?????
});
})
文档将被添加到集合主题中,这是文档的样子:
用户的结构是 (collection) users > (document) user > (collection) subjects > (document) what i want to add
您可以从 auth.currentUser.uid
属性 获取用户 ID。以下是实现此目标的方法:
const q = query(subjectRef, where("Subject", "==", subject), where("Board", "==", examBoard), where("Level", "==", level));
onSnapshot(q, (snapshot) => {
snapshot.docs.forEach(doc => {
//add a document to a subcollection in the collection users
const auth = getAuth();
var q = doc.data()
const colRef = collection(db, "users", auth.currentUser.uid,"subjects");
addDoc(colRef, {
Board:q.Board
Level:q.Level
Subject:q.Subject
});
});
在我的代码中,我从现有的 Firebase 集合中获取了一个对象,然后我想将它作为一个对象添加到另一个文档中,但是我不知道该怎么做
// Create a query against the collection.
const q = query(subjectRef, where("Subject", "==", subject), where("Board", "==", examBoard), where("Level", "==", level));
onSnapshot(q, (snapshot) => {
snapshot.docs.forEach(doc => {
//add a document to a subcollection in the collection users
const auth = getAuth();
//editted so onAuthState function is not called
const uid = auth.currentUser.uid
const docRef = doc(db, "users", uid);
const colRef = collection(docRef, "subjects");
addDoc(colRef, {
//?????
});
})
文档将被添加到集合主题中,这是文档的样子:
您可以从 auth.currentUser.uid
属性 获取用户 ID。以下是实现此目标的方法:
const q = query(subjectRef, where("Subject", "==", subject), where("Board", "==", examBoard), where("Level", "==", level));
onSnapshot(q, (snapshot) => {
snapshot.docs.forEach(doc => {
//add a document to a subcollection in the collection users
const auth = getAuth();
var q = doc.data()
const colRef = collection(db, "users", auth.currentUser.uid,"subjects");
addDoc(colRef, {
Board:q.Board
Level:q.Level
Subject:q.Subject
});
});