Firestore 컬렉션에서 문서 ID를 가져오는 중
아이디로 서류를 검색하려고 하는데 알 수가 없어요.
현재 다음과 같이 문서를 검색합니다.
const racesCollection: AngularFirestoreCollection<Races> = this.afs.collection('races');
return racesCollection.valueChanges();
나는 내 문서 목록을 완벽하게 받긴 하지만, 그 문서들에는 did가 없습니다.
각 문서에 대해 어떻게 검색할 수 있습니까?
Angular 8 및 Firebase 6의 경우 옵션 ID 필드를 사용할 수 있습니다.
getAllDocs() {
const ref = this.db.collection('items');
return ref.valueChanges({idField: 'customIdName'});
}
지정된 키(customIdName)를 가진 개체에 문서 ID를 추가합니다.
컬렉션의 문서 ID를 가져오려면 다음을 사용해야 합니다.snapshotChanges()
this.shirtCollection = afs.collection<Shirt>('shirts');
// .snapshotChanges() returns a DocumentChangeAction[], which contains
// a lot of information about "what happened" with each change. If you want to
// get the data and the id use the map operator.
this.shirts = this.shirtCollection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Shirt;
const id = a.payload.doc.id;
return { id, ...data };
});
});
설명서 https://github.com/angular/angularfire2/blob/7eb3e51022c7381dfc94ffb9e12555065f060639/docs/firestore/collections.md#example
드디어 해결책을 찾았습니다.빅터는 문서 자료와 가까웠습니다.
const racesCollection: AngularFirestoreCollection<Race>;
return racesCollection.snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Race;
data.id = a.payload.doc.id;
return data;
});
});
ValueChanges()에는 메타데이터가 포함되어 있지 않으므로 문서 ID가 필요할 때 SnapshotChanges()를 사용하고 여기에 명시된 대로 올바르게 매핑해야 합니다. https://github.com/angular/angularfire2/blob/master/docs/firestore/collections.md
각도 6+의 경우
this.shirtCollection = afs.collection<Shirt>('shirts');
this.shirts = this.shirtCollection.snapshotChanges().pipe(
map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Shirt;
const id = a.payload.doc.id;
return { id, ...data };
});
})
);
doc.idUID를 가져옵니다.
다음과 같이 하나의 개체에 대해 나머지 데이터와 결합합니다.
Object.assign({ uid: doc.id }, doc.data())
angularFire를 사용하고 있기 때문에 구현을 위한 기본 Firebase 메서드로 돌아가는 것은 의미가 없습니다.AngularFire 자체에는 적절한 메커니즘이 구현되어 있습니다.그냥 사용하면 됩니다.
valueChanges()method of angularFire는 객체를 메소드에 매개 변수로 추가하기만 하면 컬렉션의 각 문서의 ID를 가져오기 위한 오버로드를 제공합니다.
valueChanges({ idField: 'id' })
여기서 'idField'는 그대로여야 합니다.'id'는 문서 ID를 호출할 수 있습니다.
그러면 반환된 배열의 각 문서 개체는 다음과 같이 나타납니다.
{
field1 = <field1 value>,
field2 = <field2 value>,
..
id = 'whatEverTheDocumentIdWas'
}
그런 다음 이름을 지정한 필드를 참조하여 문서 ID를 쉽게 가져올 수 있습니다.
Angular Fire 5.2.0
데이터베이스에 문서를 추가하기 전에 ID를 가져올 수 있습니다.
var idBefore = this.afs.createId();
console.log(idBefore);
컬렉션이 아닌 문서 참조의 경우 다음이 필요합니다.
// when you know the 'id'
this.afs.doc(`items/${id}`)
.snapshotChanges().pipe(
map((doc: any) => {
const data = doc.payload.data();
const id = doc.payload.id;
return { id, ...data };
});
~하듯이.valueChanges({ idField: 'id'});여기서는 작동하지 않습니다.일반적으로 id로 문서를 검색하기 때문에 구현되지 않은 것 같습니다...
먹어봤어요.
return this.db.collection('items').snapshotChanges().pipe(
map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Item;
data.id = a.payload.doc.id;
data.$key = a.payload.doc.id;
return data;
});
})
);
언급URL : https://stackoverflow.com/questions/46900430/firestore-getting-documents-id-from-collection
'programing' 카테고리의 다른 글
| 정렬되지 않은 목록을 드롭다운 메뉴로 만들기 (0) | 2023.06.11 |
|---|---|
| XLS(Excel) 파일에서 데이터를 읽는 방법 [Java, Android] (0) | 2023.06.11 |
| 오라클에서 열의 데이터 유형 변경 (0) | 2023.06.11 |
| Twiny를 사용할 때 Python Matplotlib 그림 제목이 축 레이블과 겹칩니다. (0) | 2023.06.11 |
| UI 탭 표시줄 항목 이미지를 아래로 이동하시겠습니까? (0) | 2023.06.11 |