The problem that arises for sure is very simple, what I present in the code is something done with Firebase
that returns all the data of my DB
and places them in a table.
At the end in "Total"
I assigned the first "amount" to then add the next and enter "cellTotal"
, but instead of adding me concatenated as if all amount was a string
, being that from DB
comes to me as a whole and "Total"
I initialize it as a int
.
I would appreciate the help.
Edit:
At the time I was concatenated because I put cellTotal += nuevoServicio.monto
, but in the following way it only shows me the last object of the base total = total + nuevoServicio.monto; cellTotal.innerText = total;
if I have added the requested.
//Mantengo actualizada la tabla de servicios
dbSer.on('child_added', snap => {
var nuevoServicio = snap.val();
let cellTotal= document.querySelector('#totalImpuesto');
let total = 0;
let tb = document.querySelector('#tb-body');
let tr = document.createElement('tr');
let td1 = document.createElement('td');
let td2 = document.createElement('td');
let text1 = document.createTextNode(nuevoServicio.servicio);
let text2 = document.createTextNode(nuevoServicio.monto);
td1.appendChild(text1);
td2.appendChild(text2);
tr.appendChild(td1);
tr.appendChild(td2)
tb.appendChild(tr);
total = total + nuevoServicio.monto;
cellTotal.innerText = total;
})
The result of a console.log(JSON.stringify(nuevoServicio))
returns me:
{"monto":3000,"servicio":"Casa"}
{"monto":200,"servicio":"Gas"}
{"monto":350,"servicio":"Luz"}
Which is clearly an INT.
And when adding:
total = total + parseInt(nuevoServicio.monto, 10);
cellTotal.innerText = total
It returns 350 which is the last object of the base.