Usama Shahid

Usama Shahid

  • NA
  • 9.5k
  • 2.3m

How to get the key in Update() of Firebase record in Angular

Sep 5 2018 2:05 PM
Hy Friends,
 
I'm developing CRUD application with Angular using Firebase database. I've already seen many tutorials and articles, but it doesn't make any sense to me. Most of them defining a Model class with key attribute. But in my case, I've defined the firebase schema
 
Here 1 is the key and course1 is the value. My component code is
  1. export class AppComponent {  
  2.   courses$;  
  3.   course: AngularFireList;  
  4.   
  5.   constructor(private db: AngularFireDatabase) {  
  6.     this.courses$ = db.list('/courses').valueChanges();  
  7.   }  
  8.   
  9.   add(course: HTMLInputElement) {  
  10.     if (!this.course) {  
  11.       this.course = this.getData();  
  12.     }  
  13.     this.course.push({  
  14.       name: course.value,  
  15.       price: 150,  
  16.       isLive: true,  
  17.       sections: [  
  18.         { title: 'Component'},  
  19.         { title: 'Service'},  
  20.         { title: 'Template'}  
  21.       ]  
  22.     });  
  23.     course.value = '';  
  24.   }  
  25.   
  26.   getData() {  
  27.     return this.db.list('/courses');  
  28.   }  
  29.   
  30.   update(course) {  
  31.     console.log(course);  
  32.   }  
  33. }  
And my html template code is
  1. <input type="text" (keyup.enter)="add(course)" #course>    
  2. <ul>    
  3.   <li *ngFor="let course of courses$ | async">    
  4.     {{ course.name || course }}    
  5.     <button (click)="update(course)">Update</button>    
  6.   </li>    
  7. </ul> 
Problem: How can I update and delete firebase record with Angular. I need update() and delete() method code of Angular 4+. Bundle of thanks in advance.