I need to fetch the last code in an array of objects in firebase. Then increment +1 the last code and then save the list with the objects. For now I've created a generic DAO:
//Dao generico
'use strict';
findMaxCode: function(table, callback){
var refFirebase = this.getInstanceFirebase(table);
/* Aqui busca ultimo registro na tabela */
refFirebase.orderByChild("code").on('child_added', function(snapshot) {
callback(snapshot);
});
},
saveOrUpdate: function(table, object){
var refFirebase = this.getInstanceFirebase(table);
var isSave = (object.code == 0);
/* Aqui verificar se é para salvar/atualizar */
if(isSave){
this.findMaxCode(table, function(last){
object.code = last.val().code + 1;
/* Aqui atualize a lista de array */
refFirebase.push(object);
})
}else
refFirebase.push(object);
},
What happens after you add the new record to the array. From what I understand, it is running again the logic that queries the last record and then ends up becoming a loop.
Would that be the way?