【问题标题】:Receiving [Error: Internal] in RN app when triggering a Cloud Function触发云功能时在 RN 应用程序中接收 [错误:内部]
【发布时间】:2020-08-06 01:04:07
【问题描述】:

我有一个谷歌云函数,我正在从我的 RN 应用程序调用它,但它正在返回

[错误:内部]

我已将权限设置为未经身份验证的用户,因此任何人都可以调用它 - 仅用于测试目的。当我设置为 Authenticated users 权限时,它会引发另一个错误 [Error: Unauthenticated] 即使我已通过身份验证并且我可以在我的应用程序中获取 currentUser id。

尝试搜索此错误,但它没有向我发送任何可能的解决方案,因此决定在此处发布并希望收到有助于我修复它的回复。

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

exports.createUser = functions.region('europe-west1').https.onCall(async (data, context) => {

try {

    //Checking that the user calling the Cloud Function is authenticated
    if (!context.auth) {
        throw new UnauthenticatedError('The user is not authenticated. Only authenticated Admin users can create new users.');
    }

    const newUser = {
        email: data.email,
        emailVerified: false,
        password: data.password,
        disabled: false
    }
    const role = data.role;
    const userRecord = await admin
        .auth()
        .createUser(newUser);
    const userId = userRecord.uid;
    const claims = {};
    claims[role] = true;
    await admin.auth().setCustomUserClaims(userId, claims);
    return { result: 'The new user has been successfully created.' };
} catch (error) {
    if (error.type === 'UnauthenticatedError') {
        throw new functions.https.HttpsError('unauthenticated', error.message);
    } else if (error.type === 'NotAnAdminError' || error.type === 'InvalidRoleError') {
        throw new functions.https.HttpsError('failed-precondition', error.message);
    } else {
        throw new functions.https.HttpsError('internal', error.message);
    }
}
});

在我的 RN 应用程序中,我这样称呼它:

    var user = {
                role: role
               }

    const defaultApp = firebase.app();
    const functionsForRegion = defaultApp.functions('europe-west1');
    const createUser = await functionsForRegion.httpsCallable('createUser');
    createUser(user)
            .then((resp) => {
                //Display success
             });
                console.log(resp.data.result);
            })
            .catch((error) => {
                console.log("Error on register patient: ", error)
            });

我认为我在我的 RN 应用程序中调用它的方式是正确的,因为我已经使用 testFunction 对其进行了测试,并且我返回了一个简单的字符串。所以,我相信问题出在函数本身的某个地方。

编辑:我只是通过简单地调用函数并返回上下文进行测试,它总是返回内部错误:

exports.registerNewPatient = functions.region('europe-west3').https.onCall((data, context) => {
 return context; //this is returned as INTERNAL error. 
}

我只是无法理解这里发生了什么,为什么当我作为用户身份验证时它会返回内部错误并且它应该返回经过身份验证的用户数据,不是吗?

【问题讨论】:

    标签: javascript react-native firebase-authentication google-cloud-functions react-native-firebase


    【解决方案1】:

    在您的 registerNewPatient 函数中尝试一些 console.log(context) ; console.log(data) 语句并查看日志。他们说什么?

    需要考虑的其他一些事情可能包括在您的客户端代码中使用europe-west1,而您的功能代码使用europe-west3。尝试让这些排队,看看它是否有效?根据我的经验,如果指定的函数不存在,客户端会收到一个内部错误。

    【讨论】:

    • 我刚换了 europe-west1 还是不行。我尝试记录上下文和它的空,数据正在发送。那么,上下文有问题,是我必须在触发函数时将其传递给某个地方还是它会自动发送当前用户的上下文?
    • 您能否在pastebin.com 之类的地方分享所有相关代码,并分享确切的日志是什么?上下文是 null 还是空对象?
    • 上下文是一个空对象。你还想分享什么?上面的代码重现了错误。
    • 在一个地方查看更新的代码会很有用,这通常有助于在线调试。例如,上面的代码可能已经过时了 europe-west3europe-west1 并且 createUser / registerNewPatient 彼此不同,我想看看你的 console.log()s 到底在哪里。特别是因为这是一个非常奇怪的错误,更清晰将有助于查明错误者
    猜你喜欢
    • 2018-08-03
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 2019-01-10
    • 1970-01-01
    • 2020-02-07
    • 2022-01-15
    相关资源
    最近更新 更多