Below is the working code to access data form firebase. It uses global variable 'Data' array to send it to the final callback function. But I don't want to declare global variable. So is there anyway I can pass my data to every callbacks after it ? below is the code.
var data = {};
getData('myKey', function(){
console.log("myCompleteData: "+ data); //both Id and finalData
});
var getData= function(key,callback) {
return fireproof.child("data").child(key)
.then(CUSTOM_getData)
.then(callback)
}
function CUSTOM_getData(snapshot) {
var id= snapshot.val().id;
data.id= id;
return {
then: function(callback) {
fireproof.child("otherData").child(data.id)
.then(CUSTOM_getSomethingFromId)
.then(callback)
}
};
}
function CUSTOM_getSomethingFromId(snapshot) {
var finalData = snapshot.val().finalData;
data.finalData = finalData;
return {
then: function(callback) {
callback(data);
}
};
}
And I am new to Node.js. So please let me know if this approach is correct :)