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 :)
Working code:
var getSomeData = function(key,callback) {
var data = {};
fireproof.authWithCustomToken(nconf.get('config:someToken')).then(function(){
return fireproof.child("data").child(key)
}).then(function(snapshot){
data.id= snapshot.val().id;
return fireproof.child("otherData").child(data.id)
}).then(function(snapshot){
data.finalData = snapshot.val().finalData;
callback(data);
}, function(error){
console.log('Error: '+error);
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.