首页 > 解决方案 > Firebase getUserByEmail 不返回用户信息

问题描述

我想通过getUserByEmail函数获取所有用户信息。但它是返回[object Object]

也许我做错了什么。我只需要通过用户电子邮件获取用户displayNamephotoURL 。

我的 Node.js

exports.getUserInfo = functions.https.onCall((data, context) => {

    if (context.auth.token.admin !== true) {
        return {
            message: 'permission denied. Admin role require'
        }
    }
    return admin.auth().getUserByEmail(data.email)
        .then(function (userRecord) {
            return {
                message: 'Successfully fetched user data:' + userRecord.toJSON()
            }
        })
        .catch(function (error) {
            return {
                message: 'Error fetching user data:' + error
            }
        });
});

我的 Index.js

const updateForm = document.querySelector('#update-form');

updateForm.addEventListener('submit', (e) => {
    e.preventDefault();
    const email_st = document.querySelector('#update-email').value;

    const getUserInfo = functions.httpsCallable('getUserInfo');
    getUserInfo({
        email: email_st
    }).then(result => {
        console.log(result);
    });
});

控制台日志

标签: javascriptnode.jstypescriptfirebasegoogle-cloud-functions

解决方案


您需要对对象进行字符串化才能打印到控制台。

您可以通过以下方式执行此操作:

console.log(JSON.stringify(result));

如果你想用某种格式打印它,你可以使用 -

console.log(JSON.stringify(result, null, 4));

您可以在此处阅读文档以获取更多详细信息。


推荐阅读