[英]How do you retrieve the id of an object out of an array stored in Firebase?
我正在做一個相對簡單的卡片目錄項目。 它從表單中獲取大量輸入,並將它們顯示在單獨的卡片上。
輸入創建一個 object,每個 object 都被推入數據庫,使用firebase.database().ref('/Book').push(const formed from constructor);
那么數據庫看起來像這樣:
project-library-7e667
- Book
- MDXCUa-3s5yX-Iv8u7X
contrib: "Me"
fname: "Albert"
lname: "Camus"
own: false
pubDate: "1942"
title: "L'Etranger"
每張卡片上都有刪除它的選項,或者通過復選框將own
的值更改為“true”。
是否有MDXCUa-3s5yX-Iv8u7X
事物的簡寫/參考/變量/符號的名稱?
我不能使用名稱本身,因為顯然它會隨每張卡而改變。 我想我可以用每次更改覆蓋整個數組,但我認為理解這個問題會更好。
我還想在項目刷新時從數據庫中提取以重繪每張卡片,因此很高興知道這些對象是否有引用它們的方法。
謝謝!
Okej,您要做的是在執行.data()
之前獲取文檔的密鑰
所以是這樣的:
const db = firebase.firestore();
const ref = db.collection('books').get()
ref.forEach(doc=> {
const book = {
id: doc.id,
author: doc.data().author,
body: doc.data().body,
title: doc.data().title,
starCount: doc.data().starCount,
authorPic: doc.data().authorPic
}
//set your state here with the "book" object
})
從文檔...
https://firebase.google.com/docs/database/web/read-and-write
function writeNewPost(uid, username, picture, title, body) {
// A post entry.
var postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0,
authorPic: picture
};
// Get a key for a new Post.
var newPostKey = firebase.database().ref().child('posts').push().key;
// Write the new post's data simultaneously in the posts list and the user's post list.
var updates = {};
updates['/posts/' + newPostKey] = postData;
updates['/user-posts/' + uid + '/' + newPostKey] = postData;
return firebase.database().ref().update(updates);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.