Skip to content

Instantly share code, notes, and snippets.

@kevivforever
Last active May 30, 2019 06:15
Show Gist options
  • Save kevivforever/20e4b9551ffcf9cf5e3f9716ae491dd7 to your computer and use it in GitHub Desktop.
Save kevivforever/20e4b9551ffcf9cf5e3f9716ae491dd7 to your computer and use it in GitHub Desktop.
Firebase cloud Function to read data change on one child. Join data from two childs and write to third child
//Read data change on your ref
exports.functionname = functions.database.ref('/Items/{itemid}')
.onWrite(event => {
const item = event.data.val();
// read data once from second node
const locationRef = event.data.adminRef.root.child('location').child(event.params.itemid);
locationRef.once('value').then(snapshot =>{
const location = snapshot.val();
const city = location.city;
// data node where you want to write data
const CityRef = event.data.adminRef.root.child(city);
return CityRef.set({
id: item.id,
name: item.name,
imageUrl: item.imageUrl,
price: item.price,
type: item.type,
latitude: location.latitude,
longitude: location.longitude
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment