【问题标题】:how to delete particular record based on firebase json key如何根据firebase json键删除特定记录
【发布时间】:2020-06-03 23:07:36
【问题描述】:

显示数据的 HtmlCode。键作为参数传递

<div class="col-xs-12 col-md-6 col-md-offset-3" *ngIf="loadedPosts.length >= 1>
  <ul class="list-group">
    <li class="list-group-item" *ngFor="let post of loadedPosts">
      <h3>Title: {{ post.title}}</h3>
      <span>Content: {{ post.content}}</span>
      <p> <a (click)="onDeletePost(post.id)" style="cursor: pointer;">Delete</a></p>
    </li>
  </ul>
</div>

在获取帖子时:

 onFetchPosts() {
    this.isLoading = true;

    this.postService.fetchPosts().subscribe(
        (posts)=>{
        this.isLoading = false;
        this.loadedPosts = posts;
        },(error)=>{
          this.error = error.error;
        }
    );
  }

Json 键作为参数

删除帖子时:

  onDeletePost(key:string) {
    if(confirm('Are you sure?')){
        this.postService.deletePost(key).subscribe(
          (responseData)=>{
            console.log(responseData);
            this.onFetchPosts();  
          }
        );
      }
  }

    deletePost(key:string) {
        return this.http.delete<void>(
            'https://ng-complete-guide-257c0.firebaseio.com/posts.json',{
                params: new HttpParams().set('name', key)
            }// even passing parameter it clears all record instead of particular record 
        );    
    }

【问题讨论】:

  • 对象中似乎没有 id 属性。您为什么不尝试使用AngularFireDatabase 来访问和删除条目?

标签: angular firebase firebase-realtime-database angular-http


【解决方案1】:

当您针对'https://ng-complete-guide-257c0.firebaseio.com/posts.json 触发HTTP DELETE 请求时,数据库将删除整个posts 节点。如果您想删除特定帖子,您需要针对'https://ng-complete-guide-257c0.firebaseio.com/posts/yourpostkey.json 发起删除请求。

如果您不知道要删除的帖子的键,则首先必须执行查询以确定该键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-21
    • 2021-12-17
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 2019-07-04
    相关资源
    最近更新 更多