目前,我有一个用户数据库。每个文档ID在注册时都设置为用户的首字母名称,但是,该用户名可能会随着时间的推移而改变,但文档名称不会改变。
我的问题是,如何通过名称抓取文档并将其删除?
我尝试使用
"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();
});
<小时/>
您不需要使用 Query为此(使用 where()
定义一个查询)。
您只需指向文档(即定义 DocumentReference
)并调用 delete()
方法如下。
db.collection('Users').doc(this.user.uid).delete();
关于javascript - 当 ID 设置为未在任何地方预定义的名称时删除 Firestore 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60375413/