Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active July 29, 2022 15:58
Show Gist options
  • Save katowulf/6099042 to your computer and use it in GitHub Desktop.
Save katowulf/6099042 to your computer and use it in GitHub Desktop.
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); }
});
});
}
@rojagit
Copy link

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
Copy link

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
Copy link

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
Copy link

@gigocabrera
Copy link

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