Last active
May 30, 2019 06:15
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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