1
I’m trying to create an application with Angular and Firebase, followed a guide I found on the internet, but the data is not being stored in the database!
Follows the code of the component akael.service.ts
import { Injectable } from '@angular/core';
import{ AngularFireDatabase, AngularFireList } from 'angularfire2/database'
import { Akael } from './akael.model'
@Injectable()
export class AkaelService {
selectedAkael : Akael = new Akael();
akaelList: AngularFireList<any>;
constructor(private firebase : AngularFireDatabase) {
}
getData(){
this.akaelList = this.firebase.list('models');
return this.akaelList;
}
insertAkael(akael : Akael){
this.akaelList.push({
nome: akael.nome,
sigla: akael.sigla,
descricao: akael.descricao
});
}
updateAkael(emp : Akael){
this.akaelList.update(emp.$key,{
nome: emp.nome,
sigla: emp.sigla,
descricao: emp.descricao
})
}
deleteAkael(key : string){
this.akaelList.remove(key);
}
}
Code of the Register-model.ts component
import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms'
import { AkaelService } from '../shared/akael.service'
@Component({
selector: 'app-register-model',
templateUrl: './register-model.component.html',
styleUrls: ['./register-model.component.css']
})
export class RegisterModelComponent implements OnInit {
constructor(private akaelService: AkaelService) { }
ngOnInit() {
this.resetForm();
}
onSubmit(form: NgForm){
if(form.value.$key == null)
this.akaelService.insertAkael(form.value);
else
this.akaelService.updateAkael(form.value);
this.resetForm(form);
}
resetForm(form? : NgForm){
if(form != null)
form.reset();
this.akaelService.selectedAkael = {
$key: null,
nome: '',
sigla: '',
descricao: '',
}
}
onDelete(form: NgForm){
if(confirm('Are you sure to delete this record? ')==true)
{
this.akaelService.deleteAkael(form.value.$key);
this.resetForm(form);
}
}
}
Check your security rules. Make sure they allow you to read and write data
– Rosário Pereira Fernandes