【问题标题】:Using Firebase cloud functions callable使用可调用的 Firebase 云函数
【发布时间】:2020-04-18 23:30:43
【问题描述】:

我正在尝试使用 admin sdk 检查用户电话号码。当我在数据库中检查一个数字时,它会显示结果,但是当我输入一个不在数据库中的数字时,它会引发一个内部错误。

下面是函数 index.js 的示例代码

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

          exports.checkPhoneNumber = functions.https.onCall(async (data, context) => {
            const phoneNumber = await admin.auth().getUserByPhoneNumber(data.phoneNumber);
            return phoneNumber;
          })

前端.js

          toPress = () => {
            const getNumberInText = '+12321123232';

            const checkPhone = Firebase.functions.httpsCallable('checkPhoneNumber');
            checkPhone({ phoneNumber: getNumberInText }).then((result) => {
              console.log(result);
            }).catch((error) => {
              console.log(error);
            });
          }

以下是我输入一个不在身份验证中的数字时遇到的错误

- node_modules\@firebase\functions\dist\index.cjs.js:59:32 in HttpsErrorImpl
- node_modules\@firebase\functions\dist\index.cjs.js:155:30 in _errorForResponse

- ... 14 more stack frames from framework internals

【问题讨论】:

  • 你能分享你得到的错误吗?另外你为什么不做一个简单的查询来检查电话号码,如果你得到一个空的响应,这意味着这个号码不存在。这可能会为您省去麻烦。
  • 我已经在上面的描述中添加了错误是什么意思做一个简单的查询来检查电话号码,如果你得到一个空的响应意味着这个号码不存在?

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


【解决方案1】:

正如您将在 Callable Cloud Functions 的 documentation 中看到的那样:

如果服务器抛出错误或结果的 promise 被拒绝,则客户端会收到错误。

如果函数返回的错误是function.https.HttpsError 类型,那么客户端会收到来自服务器错误的错误代码、消息和详细信息。 否则,错误中包含消息INTERNAL和代码INTERNAL

由于您没有专门管理 Callable Cloud Function 中的错误,因此您会收到内部错误。


因此,如果您想在前端获取更多详细信息,则需要处理 Cloud Function 中的错误,如文档中here 中所述。

例如,你可以修改如下:

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

    try {
        const phoneNumber = await admin.auth().getUserByPhoneNumber(data.phoneNumber);
        return phoneNumber;
    } catch (error) {
        console.log(error.code);
        if (error.code === 'auth/invalid-phone-number') {
            throw new functions.https.HttpsError('not-found', 'No user found for this phone number');
        }
    }
})

如果getUserByPhoneNumber() 方法返回的错误代码是auth/invalid-phone-number,我们会抛出not-found 类型的错误(查看所有可能的Firebase 函数状态代码here)(查看所有可能的错误代码@987654326 @)。

您可以通过处理getUserByPhoneNumber() 返回的其他错误并将其他特定状态代码发送给客户端来优化此错误处理代码。

【讨论】:

  • 谢谢你...我已经按照你上面的解释做了,还阅读了文档,这次没有显示错误。但在firebase云功能控制台中,会显示日志。但它在前端没有显示任何内容
  • 已修复...谢谢,错误代码是用户未找到
【解决方案2】:

这是我通常用来检查我的集合中的任何文档中是否存在字段(例如电话)的一种方法。

基于您在此处描述的示例是我创建的集合:

查询手机是否存在的代码如下所示:(我使用的是 Node.Js)

let collref = db.collection('posts');

var phoneToCheck = '+123456789'
const phone1 = collref.where('phone', '==', phoneToCheck)

let query1 = phone1.get()
  .then(snapshot => {
    if (snapshot.empty) {
      console.log('No matching documents.');
      return;
    }

    snapshot.forEach(doc => {
      console.log(doc.id, '=>', doc.data());
    });
  })
  .catch(err => {
    console.log('Error getting documents', err);
  });

如果存在具有该电话号码的文档,则响应如下:

我没有文件有那个电话号码,那么响应如下:

【讨论】:

    猜你喜欢
    • 2019-01-18
    • 2018-09-16
    • 2020-07-07
    • 2020-11-24
    • 2020-05-17
    • 2021-09-13
    • 2021-02-09
    • 2021-08-29
    • 2021-01-21
    相关资源
    最近更新 更多