[英]Deleting Firestore Document when ID is set to a name which is not pre-defined anywhere
目前,我有一個用戶數據庫。 每個文檔ID在注冊時都設置為用戶的initial-name,但是這個用戶名可能會超時更改,但文檔名稱沒有。
我的問題是,如何按名稱抓取文檔並將其刪除?
我嘗試使用
"db.collection('Users').where('user_id', '==', this.user.uid)"
我在其他地方使用它來將身份驗證與其他地方的用戶配置文件相匹配,但不確定如何在拋出錯誤后直接將整個文檔刪除為“.delete()”。
有任何想法嗎?
按照下面的評論進行更新(說明實際上this.user.uid
不是 Firestore 文檔的 ID)
所以你確實應該執行一個查詢,當你得到這個查詢的結果時(在then()
方法中),刪除查詢返回的(唯一的)文檔,如下所示:
var query = db.collection('Users').where('user_id', '==', this.user.uid);
query.get()
.then(function(querySnapshot) {
var docSnapshot = querySnapshot.docs[0]; // We get the first (and unique) document of the querySnapshot
docSnapshot.ref.delete();
});
您可以直接指向文檔(即定義DocumentReference
)並調用delete()
方法,如下所示。
db.collection('Users').doc(this.user.uid).delete();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.