[英]Firebase function getting error when trying to write to firebase storage "Firebase Storage: User does not have permission to access"
[英]Is there a way to write this firebase 8 function in firebase 9?
我得到了這個 function 來將數據添加到 Firestore 數據庫,並且想知道如何在較新的版本中做到這一點。
db.doc(`User/${fields.user}/Address/${fields.address}`)
.set({
User: fields.user,
Address: fields.address,
})
.then(
db.doc(`User/${fields.user}/Address/${fields.address}/Orders/${fields.ID}`)
.set({
ID: fields.ID,
});
這個 function 是在一個集合中添加一個包含數據的文檔,然后創建一個具有不同文檔的子集合,其中包含自己的數據。 文檔 ID 是表單輸入。
您首先需要使用doc()
function 為文檔創建DocumentReference
,然后使用setDoc()
function 在 Firestore 中添加文檔,如文檔中所述。
import { doc, setDoc } from "firebase/firestore"
// here db is getFirestore()
const docRef = doc(db, `User/${fields.user}/Address/${fields.address}`)
await setDoc(docRef, { test: "test" })
或者,您可以使用批量寫入一次添加兩個文檔。 嘗試重構代碼,如下所示:
import {
writeBatch,
doc
} from "firebase/firestore";
// Get a new write batch
const batch = writeBatch(db);
const docRef = doc(db, `User/${fields.user}/Address/${fields.address}`);
batch.set(docRef, {
User: fields.user,
Address: fields.address
});
const subDocRef = doc(db, `User/${fields.user}/Address/${fields.address}/Orders/${fields.ID}`);
batch.update(subDocRef, {
ID: fields.ID
});
// Commit the batch
batch.commit().then(() => {
console.log("Documents added")
}).catch(e => console.log(e));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.