你好我试图获取我的函数返回的值,我在控制台日志中获取它们,但是如何在调用我的函数时访问它们? 功能:
function getprofile(useruid) {
return firebase.database().ref('users/'+useruid+'/')
.once('value')
.then(function(bref) {
var username= bref.val().username;
var provider= bref.val().provider;
var submitedpic=bref.val().profilepic;
var storageRef = firebase.storage().ref();
console.log("The current ID is: "+useruid+" and the current username is: "+username+'/provider is: '+provider+'/pic is :'+submitedpic);
});
}
我这样称呼我的功能:
getprofile(userid);
答案 0 :(得分:2)
您必须从.then()
回调
function getprofile(useruid) {
return firebase.database().ref('users/'+useruid+'/')
.once('value')
.then(function(bref) {
var username= bref.val().username;
var provider= bref.val().provider;
var submitedpic=bref.val().profilepic;
var storageRef = firebase.storage().ref();
console.log("The current ID is: "+useruid+" and the current username is: "+username+'/provider is: '+provider+'/pic is :'+submitedpic);
// return the values here, in the form of an object
return {
useruid: useruid,
username: username,
provider: provider,
submitedpic: submitedpic,
storageRef: storageRef
};
// or simply return the value returned by firebase
/*
return bref;
*/
});
}
.once()
会返回一个承诺,因此当您从getprofile()
获得返回值时,您将获得一个承诺,该承诺会从您的firebase调用产生实际结果:
getprofile(userid).then(function(data) {
// use data here
})