Firebase Firestore의 수집 스냅샷 항목 매핑
Firebase Firestore 가이드는 수집 스냅샷에서 문서를 반복하는 방법을 보여줍니다.forEach
:
db.collection("cities").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
});
나는 그것이 도움이 될 것이라고 생각했습니다.map
역시, 하지만 그렇지 않습니다.스냅샷을 매핑하려면 어떻게 해야 합니까?
답은 다음과 같습니다.
querySnapshot.docs.map(function(doc) {
# do something
})
Firestore의 Reference 페이지에는 다음이 표시됩니다.docs
스냅샷의 속성입니다.
문서 non-null이 아닌 화재 기지의 배열입니다.소방서스냅샷 문서화
쿼리 스냅샷에 있는 모든 문서의 배열입니다.
파이어스토어가 그들의 수업에서 물건을 돌려주는 것에 꽤 질렸습니다.여기 당신이 그것을 주면 도와주는 사람이 있습니다.db
그리고.collection
실제 배열을 확인하는 약속으로 해당 컬렉션의 모든 레코드를 반환합니다.
const docsArr = (db, collection) => {
return db
.collection(collection)
.get()
.then(snapshot => snapshot.docs.map(x => x.data()))
}
;(async () => {
const arr = await docsArr(myDb, myCollection)
console.log(arr)
})()
// https://firebase.google.com/docs/firestore/query-data/get-data
const querySnapshot = await db.collection("students").get();
// https://firebase.google.com/docs/reference/js/firebase.firestore.QuerySnapshot?authuser=0#docs
querySnapshot.docs.map((doc) => ({ id: doc.id, ...doc.data() }));
여기 또 다른 예가 있습니다.
var favEventIds = ["abc", "123"];
const modifiedEvents = eventListSnapshot.docs.map(function (doc) {
const eventData = doc.data()
eventData.id = doc.id
eventData.is_favorite = favEventIds.includes(doc.id)
return eventData
})
지도를 사용하고 문서 ID를 가져오는 더 나은 방법은 다음과 같습니다.
생성자에서 업데이트할 개체 배열로 시작합니다.
this.state = {
allmystuffData: [
{id: null,LO_Name: "name", LO_Birthday: {seconds: 0, nanoseconds: 0},
LO_Gender: "Gender", LO_Avatar: "https://someimage", LO_Type: "xxxxx"},],
};
그리고 내 기능으로 다음을 수행합니다.
const profile = firebase
.firestore()
.collection("users")
.doc(user.uid)
.collection("stuff")
.get()
.then( async (querySnapshot) => {
console.log("number of stuff records for ",user.uid," record count is: ",
querySnapshot.size);
const profile = await Promise.all(querySnapshot.docs.map( async (doc) => {
const stuffData = doc.data()
stuffData.id = doc.id
condole.log("doc.id => ",doc.id)
return stuffData
}));
this.setState({allmystuffData: profile});
})
.catch(function (error) {
console.log("error getting stuff: ", error);
})
이 예에서는 쿼리 스냅샷을 사용하여 컬렉션에 있는 모든 문서를 매핑할 때 읽었습니다.약속모두를 선택하면 모든 레코드가 화면에 렌더링되기 전에 반환됩니다.반환된 배열의 각 개체의 "id" 요소에 문서 ID를 추가한 다음 setstate를 사용하여 쿼리에서 반환된 배열로 상태 배열을 바꿉니다.
이것을 시도해보세요.
FirebaseFirestore.instance
.collection('Video_Requests')
.get()
.then((QuerySnapshot querySnapshot){querySnapshot.docs.forEach((doc){
print(doc.data());
});
});
언급URL : https://stackoverflow.com/questions/46614055/map-items-of-collection-snapshot-in-firebase-firestore
'programing' 카테고리의 다른 글
해당 값으로 연결된 키 선택 (0) | 2023.07.11 |
---|---|
VBA 웹 서버에서 UTF-8 CSV 파일 가져오기 (0) | 2023.07.11 |
잘못된 정방향 참조 또는 컴파일되지 않은 형식에 대한 참조 (0) | 2023.07.11 |
부하가 있는 동안 기본값으로 열을 추가하는 가장 좋은 방법 (0) | 2023.07.11 |
PL/SQL 저장 프로시저의 AST(추상 구문 트리)에 액세스하려면 어떻게 해야 합니까? (0) | 2023.07.11 |