Skip to content

Instantly share code, notes, and snippets.

@nguyenvanhaivn
Forked from jofftiquez/firebase_copy.js
Created July 11, 2019 08:42
Show Gist options
  • Select an option

  • Save nguyenvanhaivn/eadb047e515d8148ce4d95eb5339aba0 to your computer and use it in GitHub Desktop.

Select an option

Save nguyenvanhaivn/eadb047e515d8148ce4d95eb5339aba0 to your computer and use it in GitHub Desktop.
Firebase realtime database - how to copy or move data to a new path?
function copyFbRecord(oldRef, newRef) {
return Promise((resolve, reject) => {
oldRef.once('value').then(snap => {
return newRef.set(snap.val());
}).then(() => {
console.log('Done!');
resolve();
}).catch(err => {
console.log(err.message);
reject();
});
});
}
function moveFbRecord(oldRef, newRef) {
return Promise((resolve, reject) => {
oldRef.once('value').then(snap => {
return newRef.set(snap.val());
}).then(() => {
return oldRef.set(null);
}).then(() => {
console.log('Done!');
resolve();
}).catch(err => {
console.log(err.message);
reject();
});
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment