Firebase Firestore 在文档删除失败时返回 true [英] Firebase Firestore returning true on failed document delete
问题描述
我有一个使用 Firebase 的 Firestore 进行持久化的应用.我正在使用 Material 的 Snackbar 进行通知.我有我的服务..
I have an app which uses Firebase's Firestore for persistence. I am using Material's Snackbar for notifications. I have in my service..
deleteProvider(data) {
return this.db.collection(data.table).doc('xzc').delete();
}
其中数据参数只是一个对象,其中包含我要对其执行操作的表和我要删除的文档的 ID.
Where the data param is simply an object containing the table which I want to perform the action on and the id of the document I wish to delete.
data: { table: 'providers', id: 'some firebase identifier'}
在我的组件中
onDeleteEntry(event) {
if (event.table === 'providers') {
this.providerService.deleteProvider(event)
.then(() => {
this.snackBar.open('Provider deleted');
})
.catch((error) => {
this.snackBar.open('Error deleting provider', error);
});
}
}
这完美地删除了文档,但在测试失败的情况时,它返回真.
This deletes the document perfectly, but on testing the failed case, it returns true.
例如,如果我将 'xyz' 硬编码到 .doc('xyz') 方法中.响应调用我的 next() 方法,而不是我的 catch().
For example, if I hard-code 'xyz' into the .doc('xyz') method. The response calls my next() method, rather than my my catch().
deleteProvider(data) {
return this.db.collection(data.table).doc('xyz').delete();
} // returns true on a non-existent document
Firebase 文档强调嵌套集合在删除后仍将保留的事实,但除此之外,删除文档的基本功能很简单,除了
The Firebase docs emphasise the fact that nested collections will remain after deletion but other than that, the basic functionality of deleting a document is simple and doesn't give very much information other than
db.collection("cities").doc("DC").delete().then(function() {
console.log("Document successfully deleted!");
}).catch(function(error) {
console.error("Error removing document: ", error);
});
我错过了什么?
TIA
推荐答案
目前,如果文档不存在,delete
不会不会失败.在某种程度上,这与 create
不一致,如果事先存在,它确实会失败.
Currently, delete
does not fail if a document doesn't exist. In a way, this is inconsistent with create
, which does fail if it exists beforehand.
但是,此功能似乎是/曾经计划的.在 Java SDK 中,delete
可以带一个 Precondition
对象:
However, it looks like this feature is/was planned. In Java SDK, delete
can take a Precondition
object:
/**
* Deletes the document referred to by this DocumentReference.
*
* @param options Preconditions to enforce for this delete.
* @return An ApiFuture that will be resolved when the delete completes.
*/
@Nonnull
public ApiFuture<WriteResult> delete(@Nonnull Precondition options) {
WriteBatch writeBatch = firestore.batch();
return extractFirst(writeBatch.delete(this, options).commit());
}
在Precondition
中,有一个辅助方法:
In Precondition
, there is a helper method:
/**
* Creates a Precondition that enforces that a document exists.
*
* @param exists Whether the document should exist.
* @return A new Precondition
*/
// TODO: Make public once backend supports verify.
@Nonnull
static Precondition exists(Boolean exists) {
return new Precondition(exists, null);
}
请注意,这不是公开的.评论说它缺乏后端支持.只有 Firestore 团队知道它的当前状态.
Note that is not public. The comment says it lacks backend support. Only Firestore team knows the current status of it.
这篇关于Firebase Firestore 在文档删除失败时返回 true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!