从另一个函数中的promise内部返回一个值

时间:2018-03-01 09:47:11

标签: javascript node.js firebase promise es6-promise

我正在尝试从firebase中的getUserRecordFromAuth函数内部返回userRecord,并使用它从Firebase中的身份验证中获取用户的UID

问题是userRecord.uid总是返回undefined,即使我在getUserRecordFromAuth()中返回了promise的值。是什么给了什么?

const getUserData = mobile => {
    const userRecord = getUserRecordFromAuth(mobile);

    console.log('uid', userRecord.uid);
}

const getUserRecordFromAuth = mobile => {

    const promise = auth.getUserByPhoneNumber(mobile);

    return promise.then(userRecord => {
        return userRecord;
    }).catch(error => {
        console.log(error);
    });
}

1 个答案:

答案 0 :(得分:2)

当您返回Promise以获得结果时,您需要使用.then()

const getUserData = mobile => {
    getUserRecordFromAuth(mobile)
     .then((userRecord) => console.log('uid', userRecord.uid));
}