angular - 无法从 Angular 应用页面按 id 删除 Firestore 文档
问题描述
我正在尝试从我的 Angular Web 应用程序中按 id 删除一个 Firestore 文档,但无法这样做。
.ts 文件
deleteStory(docId)
{
console.log("Doc id :", +docId);
this.storiesRef.doc(docId).delete().then(function() {
console.log("Document successfully deleted!");
}).catch(function(error) {
console.error("Error removing document: ", error);
});
}
ngOnInit() {
const container = document.getElementById('container');
this.afs.collection('stories', ref => ref.where('userid', '==', this.userId))
.get().toPromise()
.then(snapshot => {
snapshot.forEach(doc => {
var date = doc.data() && doc.data().time && doc.data().time.toDate();
//Create a card element
const card = document.createElement('div');
// card.classList.add('card-body');
//Construct card content
const content = `
<div data-parent="#storycontainer" style="width: 27rem;" class ="mx-auto mb-3">
<div class="card">
<div class="card-body">
<h5 class="card-title">${doc.data().storytitle}</h5>
<audio _ngcontent-cjb-c49="" controls="">
<source _ngcontent-cjb-c49="" type="audio/webm"
src=${doc.data().readyfilepath}>
</audio>
<p class ="text-sm-left">${date}</p>
<button class="btn btn-danger" (click)="deleteStory(${doc.id})">
<span class="glyphicon glyphicon-record"></span>Delete
</button>
</div>
</div>
</div>
`;
// Append newyly created card element to the container
container.innerHTML += content;
单击 html 按钮时,我在浏览器控制台中看不到任何错误。任何帮助将非常感激。
解决方案
谢谢,我在网上做了一些搜索,下面的代码帮助了我......
.ts
<button class="btn btn-secondary rounded-circle" (click)="delete(story.payload.doc.id)">
<span class="fa fa-trash" aria-hidden="true"></span>
</button>
.html
delete(id: string){
this.storiesService.deleteStory(id)
.then(
res => {
this.router.navigate(['/dashboard']);
},
err => {
console.log(err);
}
)
}
}
推荐阅读
- javascript - React 原生动画进度条
- python - “InlineKeyboardButton”类型的对象不是 JSON 可序列化的
- arrays - 将 2 个数组数据类型列转换为 Snowflake 中的行
- mysql - mariadb 无法远程连接,或者如果提供了 IP,则无法从同一台机器连接
- string - Rust 中的 `str` 有什么用处吗?
- macos - “STM32CubeIDE”已损坏,无法打开。你应该把它移到废纸篓。(在 Mac 上)
- oracle - 从 Oracle 中的 Timestamp 数据类型列中删除数据不起作用
- python - 模式匹配与 Python 中的字符/数字模式
- vue.js - Vue 组件在部署后不渲染插槽内容
- kdb - KDB 中的分页选项