[英]How to get document ID firestore angular?
我想更新firestore中文檔中的一個字段。 但是,文檔 ID 是必需的,並且我的所有文檔 ID 都是自動生成的。 我有一個名為 certName 的字段,它是唯一的,如何通過匹配 certName 字段來獲取文檔 ID? 我已經嘗試了下面的代碼,但它顯示以下錯誤。
updateDb(FILE_NAME,UPDATE_FIELD){
const ref = this.afs.collection('history', ref =>
ref.where('certName', '==', FILE_NAME));
ref.snapshotChanges().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data();
const id = a.payload.doc.id;
return { id };
}))).subscribe((_doc: any) => {
let id = _doc[0].payload.doc.id; //first result of query [0]
this.afs.doc(`options/${id}`).update({transactionId: UPDATE_FIELD});
})
這是錯誤
ERROR TypeError: Cannot read property 'payload' of undefined
at SafeSubscriber._next (db-service.service.ts:58)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.next (Subscriber.js:122)
at Subscriber._next (Subscriber.js:72)
at Subscriber.next (Subscriber.js:49)
at MapSubscriber._next (map.js:35)
at MapSubscriber.next (Subscriber.js:49)
at Notification.observe (Notification.js:20)
at AsyncAction.dispatch (observeOn.js:25)
at angular-fire.js:54
updateDb(FILE_NAME,UPDATE_FIELD) {
let docID;
this.afs.collection('history', ref => ref.where('certName', '==', FILE_NAME)).valueChanges().subscribe(res => docID = res.id);
// (res.id) - id is the document id, the document id should be stored in the collection as a seperate field
this.afs.collection('options').doc(docID).update({transactionId: UPDATE_FIELD});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.