首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用firebase数据库访问函数返回值

使用firebase数据库访问函数返回值
EN

Stack Overflow用户
提问于 2018-04-14 10:26:41
回答 1查看 3K关注 0票数 3

hello试图获取函数返回的值,我在控制台日志中获得它们,但是在调用函数时如何访问它们?职能:

代码语言:javascript
复制
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);
    });
}

我这样称呼我的职能:

代码语言:javascript
复制
 getprofile(userid);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-14 10:33:42

您必须从.then()回调中返回一个值。

代码语言:javascript
复制
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调用中得到实际结果:

代码语言:javascript
复制
getprofile(userid).then(function(data) {
    // use data here
})
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49830415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档