Unhandled Error gets thrown in Firebase Cloud Functions

你离开我真会死。 提交于 2020-08-19 17:40:25

问题


I'm using Firebase Cloud Functions, and in one of my projects I have a simple function that looks like the following:

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();
});

I know where the error is through other methods and why it is, I'm using an invalidStudentPath. But the bigger issue is that the error that gets thrown is this for all of my errors in this project:

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

How can I get the actual error instead of this obscure message? Thanks for any help.

Update


I have found a workaround for now. Currently I'm wrapping my whole function in a try catch block that looks like this:

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

回答1:


The issue was fixed in firebase-functions v3.9.1. Alternatively you could downgrade to v3.8.0.

See https://github.com/firebase/firebase-functions/issues/757#issuecomment-673080726




回答2:


I just got the same error on a new project. Took me a few hours to realize I forgot to initialize firebase admin after I imported it in my functions

import * as admin from 'firebase-admin';

admin.initializeApp();


来源:https://stackoverflow.com/questions/63349136/unhandled-error-gets-thrown-in-firebase-cloud-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!