1
I’m trying to save a result from a simple sum in Firebase but I can’t recover the value and insert it. Where I’m missing?
Home.html
<ion-content padding class="content">
<form [formGroup]='form' class="principal">
<section class="index">
<ion-list>
<ion-item>
<ion-label floating> Km inicial </ion-label>
<ion-input formControlName="kminicial" [(ngModel)]="kminicial"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Km Final</ion-label>
<ion-input formControlName="kmfinal" [(ngModel)]="kmfinal"></ion-input>
</ion-item>
</ion-list>
</section>
</form>
<section class="botaoCalc">
<button ion-button round outline (click)="calcular()"> Calcular </button>
</section>
</ion-content>
Home.ts
@Component({
selector: 'page-consumo',
templateUrl: 'consumo.html',
})
export class ConsumoPage {
title: string;
form: FormGroup;
resultkm: number;
constructor(
public navCtrl: NavController,
public toastCtrl: ToastController,
public alertCtrl: AlertController,
public navParams: NavParams,
private formBuilder: FormBuilder,
public provider: Provider) {
this.consumo = this.navParams.data.consumo || {};
this.setupPageTitle();
this.createForm();
this.consumos = this.provider.getAll();
}
private setupPageTitle() {
this.title = this.navParams.data.consumo ? 'Alterando Consumo' : 'Novo Consumo';
}
createForm() {
this.form = this.formBuilder.group({
key: [this.consumo.key],
kminicial: [this.consumo.kminicial],
kmfinal: [this.consumo.kmfinal],
resultkm: [this.resultkm]
});
console.log('entrou no create form');
}
calcular() {
this.resultkm = this.consumo.kmfinal - this.consumo.kminicial;
console.log(this.consumo.result)
this.provider.save(this.form.value)
.then(() => {
this.alertCtrl.create({
title: 'Resultado',
message:
'Abastecimento: ' + this.consumo.dataAbs +
'<br>Re-abastecimento: ' + this.consumo.dataReAbs +
'<br>Total km rodados: ' + this.resultkm,
buttons: ['OK'],
}).present();
})
.catch(() => {
this.toastCtrl.create({ message: 'Ocorreu um erro', duration: 2000 }).present();
this.navCtrl.pop();
})
}
}
}
Provider
@Injectable()
export class Provider {
private PATH = 'consumo/'
constructor(private db: AngularFireDatabase) {
console.log('Hello ** consumo ** Provider');
}
save(consumo: any) {
return new Promise((resolve, reject) => {
if (consumo.key) {
this.db.list(this.PATH)
.update(consumo.key, {
kminicial: consumo.kminicial,
kmfinal: consumo.kmfinal,
resultkm: consumo.result,
})
.then(() => resolve())
.catch((e) => reject(e));
} else {
this.db.list(this.PATH)
.push({
kminicial: consumo.kminicial,
kmfinal: consumo.kmfinal,
resultkm: consumo.result,
})
.then((result: any) => resolve(result.key))
}
})
}
remove(key: string){
return this.db.list(this.PATH).remove(key);
}
getAll() {
return this.db.list(this.PATH, ref => ref.orderByChild('dataAbs'))
.snapshotChanges()
.map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
})
}
getOne(key: string) {
return this.db.object(this.PATH + key)
.snapshotChanges()
.map(c => {
return { key: c.key, ...c.payload.val() }
})
}
}