Modify Instance from another Instance in the same Database. Firebase

0

in this opportunity I have a small requirement that I have not been able to carry out.

I have a database girl structured in this way;

in AP a data will be loaded every day, which will always occupy the last positions obviously, I need that this last data also be loaded go to the instance SA .

Try something like this;

AP.on('value',(snap)=>{
    let ap_val = snap.val();
    let ap_val_l = ap_val.length -1;
    SA.push(ap_val[ap_val_l]);
});

where through the event on then every time you change AP in this case in the daily load of the last item, with push add it to SA which is my purpose. and it really works, the problem is that every time the page loads it does this task, and it is not the objective.

What I need is that every time that AP is modified, the new data is loaded to SA only once and so to the next day again when AP the daily data is loaded.

    
asked by Gabriela 06.09.2018 в 22:01
source

1 answer

1

What you could do is check if that data with that key that would be 0,1,2,3,4,5 as you have it in the graph already exists, since how are you using the on event? will run every time you restart the page.

This way, before doing push, you verify and if it does not exist you add it: D.

I think the code would be like that.

//LimitToLast(1): Obtener el ultimo valor
AP.limitToLast(1).on('value', (snap) => {
    let ap_key = snap.key; //Obtener la key
    let ap_value = spap.val(); //Obtener el valor
    //Verificar si la llave existe en SA
    SA.child(ap_key.toString()).once('value', function (snapshot) {
        if (!snapshot.exists()) {
            SA.push({ap_key : ap_value}); //Agregar la data en SA
        }
    });
});
    
answered by 08.09.2018 / 04:32
source