[英]Delete a document in Firestore and a file in Storage
I'm doing a web application in Angular 6 and angularfire2 . 我正在Angular 6和angularfire2中做一个Web应用程序。 My document in Firestore has a field called
fileUrl
with the link of a file in Storage. 我在Firestore中的文档有一个名为
fileUrl
的字段,其中包含存储中文件的链接。 I'm trying to delete the document first and then delete the file. 我正在尝试先删除文档,然后再删除文件。 (I don't know if I can do a batch for this)
(我不知道是否可以为此做一批)
this.competitionService.deleteCompetition(competition.id, competition.fileUrl)
.then(res => {
// Deleted
}, err => {
console.log(err);
});
From main service 从主要服务
deleteCompetition(competitionId: string, fileUrl: string) {
return new Promise<any>((resolve, reject) => {
this.competitionsCollection.doc(competitionId).delete()
.then(res => {
if (fileUrl) {
this.uploadService.deleteFile(fileUrl).then(response => {
resolve(res);
}, err => {
console.log(err);
});
resolve(res);
} else {
resolve(res);
}
}, err => {
reject(err);
});
});
}
Another service for the Storage 存储的另一项服务
getMetadata(url: string) {
const ref = this.storage.ref(`${config.storage_files}/${url}`);
return ref.getMetadata().toPromise();
}
async deleteFile(fileUrl: string) {
const fileMetadata: FileMetadata = await this.getMetadata(fileUrl);
return new Promise<any>((resolve, reject) => {
this.storage.ref(fileMetadata.fullPath).delete().toPromise()
.then(res => {
resolve(res);
}, err => {
reject(err);
});
});
}
FirebaseStorageError {code_: "storage/object-not-found", message_: "Firebase Storage: Object 'files/https:/firebasesto…4c92-3a0e-45d4-adf3-5d81900f3953' does not exist.", serverResponse_: "{↵ "error": {↵ "code": 404,↵
FirebaseStorageError {代码_:“存储/未找到对象”,消息_:“ Firebase存储:对象'文件/https:/firebasesto...4c92-3a0e-45d4-adf3-5d81900f3953'不存在。”,serverResponse _:“ {↵ “错误”:{↵“代码”:404,↵
"message": "Not Found. Could not get object"↵ }↵}", name_: "FirebaseError"}“消息”:“未找到。无法获取对象”“}↵}”,名称_:“ FirebaseError”}
I don't understand the cause of the error I am receiving. 我不明白我收到的错误原因。
My goal is, delete the document in Firestore and also, delete the file in Storage, then show a message to the user. 我的目标是删除Firestore中的文档,然后删除Storage中的文件,然后向用户显示一条消息。
You can't build a reference to a file using its public https download URL like that. 您无法使用此类文件的公共https下载URL建立对文件的引用。 You will need the path to the file in storage in order to build a reference.
您将需要存储中文件的路径以建立参考。
I suggest also storing the path in the document to make it easier to delete later. 我建议也将路径存储在文档中,以方便以后删除。 But you can also convert a download URL to a Reference by using
refFromUrl()
. 但是,您也可以使用
refFromUrl()
将下载URL转换为参考 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.