【问题标题】:Receiving [Error: NOT_FOUND] when running a Cloud Function运行云函数时收到 [错误:NOT_FOUND]
【发布时间】:2025-12-13 05:55:01
【问题描述】:

当我从我的 React-Native 应用程序调用该函数时,它会抛出此错误:[Error: NOT_FOUND]

我对其进行了研究,根据 Firebase 文档,这意味着:“找不到指定的资源,或者请求因未公开的原因被拒绝,例如白名单。

这是整个控制台日志消息:

[05:51:32] 我 | ReactNativeJS ▶︎'错误处理',{ [错误:NOT_FOUND] │ 线路:26115, │ 栏目:28, └ sourceURL: 'http://localhost:8081/index.bundle?platform=android&dev=true&minify=false' }

React-Native 代码:

firebase.functions().httpsCallable('registerNewPatient')({
  email: 'bimiiix@hotmail.com',
  password: 'bbbbbb1'
}).then((onfulfilled, onrejected) => {
  if (onfulfilled) {
    console.log("OK callback function:", onfulfilled);
  } else {
    console.log("Error callback function:", onrejected)
  }
}).catch(error => { console.log("ERror handled", error) })

云功能:

exports.registerNewPatient = functions.region('europe-west3').https.onCall((data, context) => {
    if (!data.email) throw "Missing email parameter";
    if (!data.password) throw "Missing password parameter";
    const email = data.email;
    const password = data.password;

    admin.auth().createUser({
        email: email,
        emailVerified: false,
        password: password,
        disabled: false
    })
        .then(function (userRecord) {
            registeredUser = userRecord.uid;
            console.log('Successfully created new user:', userRecord.uid);
        })
        .catch(function (error) {
            console.log('Error creating new user:', error);
        });
    return registeredUser;
});

【问题讨论】:

    标签: node.js firebase react-native firebase-authentication google-cloud-functions


    【解决方案1】:

    正如docs 中突出显示的那样:

    注意:要调用在默认us-central1 以外的任何位置运行的函数,您必须在初始化时设置适当的值。例如,在 Android 上,您可以使用 getInstance(FirebaseApp app, String region) 进行初始化。

    对于 Firebase Javascript SDK,此方法为 firebase.app.App#functions(String region)

    所以要使用上面europe-west3区域的云功能,你需要改变

    firebase.functions().httpsCallable('registerNewPatient')(/* ... */)
    

    firebase.app().functions('europe-west3').httpsCallable('registerNewPatient')(/* ... */)
    

    const functionsEUWest3 = firebase.app().functions('europe-west3');
    functionsEUWest3.httpsCallable('registerNewPatient')(/* ... */)
    

    【讨论】:

    • 山姆抓得好!
    • 现在我遇到了这个错误:[错误:内部]。我尝试从函数中只返回电子邮件+密码,它确实返回了,所以发送参数正在正确完成,问题应该在 admin.auth() 的某个地方。我真的需要一些特殊权限来创建新用户吗?如果我只使用 auth().createUserWithEmailAndPassword 有什么区别?该函数如何知道我是否是管理员?
    【解决方案2】:

    除了@samthecodingman 关于区域的出色回答之外,您还没有在代码中正确处理异步 API。当你的return registeredUser 现在运行时,registeredUser = userRecord.uid 还没有被调用。我建议将来使用一些额外的日志记录语句来解决此类行为。

    这应该更接近:

    exports.registerNewPatient = functions.region('europe-west3').https.onCall((data, context) => {
        if (!data.email) throw "Missing email parameter";
        if (!data.password) throw "Missing password parameter";
        const email = data.email;
        const password = data.password;
    
        return admin.auth().createUser({
            email: email,
            emailVerified: false,
            password: password,
            disabled: false
        })
        .then(function (userRecord) {
            return userRecord.uid;
            console.log('Successfully created new user:', userRecord.uid);
        })
        .catch(function (error) {
            console.log('Error creating new user:', error);
            throw new functions.https.HttpsError('Error creating user', error);
        });
    });
    

    【讨论】:

    • 现在我遇到了这个错误:[错误:内部]。我尝试从函数中只返回电子邮件+密码,它确实返回了,所以发送参数正在正确完成,问题应该出在admin.auth() 的某个地方。我真的需要一些特殊权限来创建新用户吗?如果我只使用 auth().createUserWithEmailAndPassword 有什么区别?该函数如何知道我是否是管理员?
    • 当您在 Cloud Functions 上运行 Admin SDK 时,默认情况下您拥有管理权限。您不能“只使用 aut(). createUserWithEmailAndPassword,因为 Admin SDK 中不存在该方法。请参阅 firebase.google.com/docs/reference/admin/node/admin.auth.Auth
    • 好的,我只是想到了别的东西。如果我什至没有在客户端应用程序中进行身份验证,我真的可以调用该函数吗?我还没有在 RN 中实现身份验证,因为我只是想测试那个功能。
    • 顺便说一句,我不想​​创建新的管理员,我想创建新的简单用户。我正在使用的功能是创建新管理员还是仅创建用户?我完全糊涂了!
    • @bmmf “如果未通过身份验证,我可以调用该函数吗?”,正如它所写的那样,是的。您将需要检查 context.auth 属性以根据调用者限制函数。 “这会创建普通用户吗?”,也是。当 Frank 提到上面的管理员时,他们指的是 Admin SDK (firebase-admin),这是一个服务器端 SDK,被视为可以完全控制您的项目的管理员。因此,除非您检查context.auth 的值,否则任何调用此函数的用户都可以创建用户。