Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Move or copy a Firebase path to a new location
function copyFbRecord(oldRef, newRef) {
oldRef.once('value', function(snap) {
newRef.set( snap.value(), function(error) {
if( error && typeof(console) !== 'undefined' && console.error ) { console.error(error); }
});
});
}
function moveFbRecord(oldRef, newRef) {
oldRef.once('value', function(snap) {
newRef.set( snap.value(), function(error) {
if( !error ) { oldRef.remove(); }
else if( typeof(console) !== 'undefined' && console.error ) { console.error(error); }
});
});
}
@yk-tanigawa

This comment has been minimized.

Copy link

@yk-tanigawa yk-tanigawa commented Jul 15, 2015

snap.val() instead of snap.value() might be better in the current version of Firebase.

@Wizek

This comment has been minimized.

Copy link

@Wizek Wizek commented Dec 24, 2015

Yes, this works with my version:

function moveFbRecord(oldRef, newRef) {    
     oldRef.once('value', function(snap)  {
          newRef.set( snap.val(), function(error) {
               if( !error ) {  oldRef.remove(); }
               else if( typeof(console) !== 'undefined' && console.error ) {  console.error(error); }
          });
     });
}
@larrytech7

This comment has been minimized.

Copy link

@larrytech7 larrytech7 commented Feb 8, 2016

can i have the java implementation of this? Thanks in advance

@angus-ics

This comment has been minimized.

Copy link

@angus-ics angus-ics commented Feb 26, 2016

Java implementation

public void moveFirebaseRecord(Firebase fromPath, final Firebase toPath)
    {
        fromPath.addListenerForSingleValueEvent(new ValueEventListener()
        {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot)
            {
                toPath.setValue(dataSnapshot.getValue(), new Firebase.CompletionListener()
                {
                    @Override
                    public void onComplete(FirebaseError firebaseError, Firebase firebase)
                    {
                        if (firebaseError != null)
                        {
                            Timber.d("moveFirebaseRecord() failed. firebaseError = %s", firebaseError);
                        }
                        else
                        {
                            Timber.d("moveFirebaseRecord() Great success!");
                        }
                    }
                });
            }

            @Override
            public void onCancelled(FirebaseError firebaseError)
            {
                Timber.d("moveFirebaseRecord() failed. firebaseError = %s", firebaseError);
            }
        });
    }
@MtlDev

This comment has been minimized.

Copy link

@MtlDev MtlDev commented Feb 26, 2016

Is that Java code a Move or Copy?

It appears to me to be a Copy, as there is no .remove() ?

@gigocabrera

This comment has been minimized.

Copy link

@gigocabrera gigocabrera commented Aug 5, 2016

Are these functions still valid for Firebase 3?

firebase_copy.js
firebase_move.js
@rojagit

This comment has been minimized.

Copy link

@rojagit rojagit commented Aug 17, 2016

This line reminds me of the reason StringUtils was created :)

           if( error && typeof(console) !== 'undefined' && console.error ) {  console.error(error); }
@pitoneux

This comment has been minimized.

Copy link

@pitoneux pitoneux commented Nov 29, 2016

instead of

newRef.set( snap.val(), function(error) {

you may want to do:

newRef.update( snap.val(), function(error) {

if you want to ADD the data instead of REPLACING all your existing child with the data you want to move.

@nguyenct

This comment has been minimized.

Copy link

@nguyenct nguyenct commented May 4, 2017

Any way to easily copy and preserve $priority for each item? Although, I have read somewhere that using $priority is not recommended anymore. However, it's useful for enabling sorting/ordering of key-value pairs in an object that absolutely need both key and value to make references to another object.

@jofftiquez

This comment has been minimized.

Copy link

@jofftiquez jofftiquez commented Jul 7, 2017

@gigocabrera

This comment has been minimized.

Copy link

@gigocabrera gigocabrera commented Dec 27, 2017

I'm looking for a Cloud Firestore version of copy and move. Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment