最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Firebase身份验证花费太多时间才能在云功能中创建用户

IT培训 admin 9浏览 0评论

Firebase身份验证花费太多时间才能在云功能中创建用户

当前,我遇到一个问题,即调用auth#createUser方法最多需要10秒才能继续调用它的Promise#then方法。我从Firebase和Google Cloud函数记录器获取这些时间戳。我觉得自己可能做错了,尽管我无法弄清楚自己做错了什么。在您说要联系Firebase支持人员之前,我已经有,他们告诉我来这里。


const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

let database = admin.firestore();

exports.createUser = functions.https.onCall((data, response) => {
    console.log(data);
    console.log(response);

    admin.auth().createUser({
        email: data.email,
        emailVerified: false,
        password: data.password,
        displayName: data.name,
        disabled: false,
    }).then(user => {
        database.collection('users').doc(data.username).set({
            uid: user.uid,
            email: data.email,
            name: data.name,
            username: data.username
        }).catch(error => {
            throw new functions.https.HttpsError(error)
        });
        console.log('The entire thing is done successfully!');
        return {
            response: user
        }
    }).catch(error => {
        throw new functions.https.HttpsError(error);
    });
    console.log('Found my way to the end of the method');
});
回答如下:

您没有正确处理诺言。 onCall函数应返回一个承诺,该承诺将使用您要返回给客户端的数据进行解析。现在,您的函数什么也不返回。 then回调内部的return语句实际上没有向客户端发送任何内容。相反,您需要做的是返回Promise链:

return admin.auth().createUser(...).then(...).catch(...)

请注意所有内容的开头。

此外,您将需要处理set()返回的承诺。仅调用catch是不够的。您还需要从then回调中返回该承诺。

[我强烈建议您学习诺言如何在JavaScript中工作-如果没有适当的处理,您的函数将无法正常工作,并且通常会以令人困惑的方式表现。

Firebase身份验证花费太多时间才能在云功能中创建用户

当前,我遇到一个问题,即调用auth#createUser方法最多需要10秒才能继续调用它的Promise#then方法。我从Firebase和Google Cloud函数记录器获取这些时间戳。我觉得自己可能做错了,尽管我无法弄清楚自己做错了什么。在您说要联系Firebase支持人员之前,我已经有,他们告诉我来这里。


const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

let database = admin.firestore();

exports.createUser = functions.https.onCall((data, response) => {
    console.log(data);
    console.log(response);

    admin.auth().createUser({
        email: data.email,
        emailVerified: false,
        password: data.password,
        displayName: data.name,
        disabled: false,
    }).then(user => {
        database.collection('users').doc(data.username).set({
            uid: user.uid,
            email: data.email,
            name: data.name,
            username: data.username
        }).catch(error => {
            throw new functions.https.HttpsError(error)
        });
        console.log('The entire thing is done successfully!');
        return {
            response: user
        }
    }).catch(error => {
        throw new functions.https.HttpsError(error);
    });
    console.log('Found my way to the end of the method');
});
回答如下:

您没有正确处理诺言。 onCall函数应返回一个承诺,该承诺将使用您要返回给客户端的数据进行解析。现在,您的函数什么也不返回。 then回调内部的return语句实际上没有向客户端发送任何内容。相反,您需要做的是返回Promise链:

return admin.auth().createUser(...).then(...).catch(...)

请注意所有内容的开头。

此外,您将需要处理set()返回的承诺。仅调用catch是不够的。您还需要从then回调中返回该承诺。

[我强烈建议您学习诺言如何在JavaScript中工作-如果没有适当的处理,您的函数将无法正常工作,并且通常会以令人困惑的方式表现。

发布评论

评论列表 (0)

  1. 暂无评论