I have written this simple cloud function to update the data in one node everytime data in another node gets updated.
exports.trending = functions.database.ref("/trending/{tick}")
.onUpdate(event => {
const ticker = event.params.tick;
const path = "/symbols/exchange/" +ticker;
console.log("Create trending list " + ticker);
const quotes_list = event.data.ref.root.child(path);
return quotes_list.on("child_changed", function(snapshot){
console.log("Passing " + event.data.ref);
console.log("Passing " + snapshot.val());
console.log("Passing " + ticker);
updateTicker(snapshot, event.data.ref, ticker);
});
});
function updateTicker(snapshot, ref, ticker){
if(snapshot.toJSON() !== null){
console.log("Rcvd " + ref);
return ref.update({last_price: snapshot.val()}).then(noUse => { console.log("Event triggered " + snapshot.child("name").val()); });
}else{
console.log("data is not available for " + ticker);
return;
}
}
Problem is: 1. on("child_changed", snapshot) ==> only contains data that is changed. Not the complete snapshot. 2. My cloud function works fine for first 5-10 mins.But if it remains idle for ~5 mins, It stops working? Is there something wrong I am doing here? Please help.