Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move/Copy Firebase Object to Another Child Node

I have a user object in firebase that I need to move to another child node on the same tree.

The structure is like so:

users -> male   -> uid -> ......KVPs..objects...etc
      -> female -> uid -> ......KVPs..objects...etc

I need to keep users separated by gender due to the nature of the app. If a user changes their gender I want to move all their details to that selected gender.

I tried using AngularFire (firebaseObject) but it throws errors when I tried to set it in firebase due to the keys present in the object. I tried striping out the keys, using JSON.stringify, angular.toJSON but am not having any luck!!

I'm wondering is there a cleaner or recommended way to do this? Appreciate If anyone has any pointers or can assist in anyway.

Many Thanks, Noel

like image 425
Noel McKeown Avatar asked Jan 05 '23 06:01

Noel McKeown


1 Answers

This is actually pretty easy

    // firebase ref
    desc.ref1 = new Firebase(desc.userRef+'/'+desc.oldGender+'/'+uid);
    desc.ref2 = new Firebase(desc.userRef+'/'+desc.gender+'/'+uid);

    desc.ref1.on("value", function(snapshot) {
        console.log(snapshot.val());

        desc.ref2.set(snapshot.val());

    }, function (errorObject) {
        console.log("The read failed: " + errorObject.code);
    });
like image 159
Noel McKeown Avatar answered Jan 13 '23 10:01

Noel McKeown