hello试图获取函数返回的值,我在控制台日志中获得它们,但是在调用函数时如何访问它们?职能:
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);发布于 2018-04-14 10:33:42
您必须从.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
})https://stackoverflow.com/questions/49830415
复制相似问题