Firebase Cloud Functions中引发了未处理的错误

问题描述

我正在使用Firebase Cloud Functions,在我的一个项目中,我有一个简单的函数,如下所示:

exports.responseGiven = functions.https.onCall(async (data,context) => {
  if (!context.auth) {
    throw new functions.https.HttpsError(
        'permission-denied','Must be an user to execute this action'
    );
  }

  const studentPath = data.studentPath;
  const eventPath = data.eventPath;
  const isUpdating = data.isUpdating;

  const studentDoc = await admin.firestore().collection('students').doc('studentPath').get();
  const eventDoc = await studentDoc.ref.collection('entries').doc(eventPath).get();
});

我通过其他方法知道错误在哪里以及为什么发生,我正在使用invalidStudentPath。但是更大的问题是,引发的错误就是我在该项目中遇到的所有错误:

Unhandled error function error(...args) {
   write(entryFromArgs('ERROR',args));
} 

如何获取实际错误而不是此晦涩的消息?感谢您的帮助。

更新


我现在找到了解决方法。目前,我将整个函数包装在一个如下所示的try catch块中:

exports.responseGiven = functions.https.onCall(async (data,context) => {
  try {
    ...
  } catch (e) {
    console.log('There was an error');
    console.log(e);
  }
});

解决方法

此问题已在firebase-functions v3.9.1中修复。另外,您可以降级到v3.8.0。

请参见https://github.com/firebase/firebase-functions/issues/757#issuecomment-673080726

,

我在一个新项目中遇到了同样的错误。花了我几个小时才意识到我在将Firebase admin导入函数后忘记了初始化它。

import * as admin from 'firebase-admin';

admin.initializeApp();

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...