Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firebase new Cloud Function throws INTERNAL error

Im working on a project in Vue.js 3 Typescript and Firebase.
When trying to implement new cloud functions i suddenly encountered the following problem:

Failed to load resource: the server responded with a status of 500 ()

Unhandled Promise Rejection: FirebaseError: INTERNAL

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My old Cloud Functions work just as expected, but trying to add one that deletes a document it suddenly occurred. I tried breaking it down thinking it was probably a syntactical problem, but even copying the whole boilerplate from another working cloud function did not help.
Next i started to strip the function of its guts.
Two things i discovered:

1. Barebones

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

  console.log('teachersRef')
  return "teachersRef"
})

Output: {data: ‘teachersRef’}

This code works as expected so there is no mistake when calling the function.

2. Just a little more

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

  const teachersRef = db.collection(db, 'schools/' + 'y70B7KSbwd2D55SRPItY' + "/teachers");
  console.log(teachersRef)

  return "test"
})

Output: Failed to load resource: the server responded with a status of 500 ()

Unhandled Promise Rejection: FirebaseError: INTERNAL

This code somehow already stops working entirely and throws the before mentioned error.
Note: The console.log is only there to have used "teachersRef" variable in order to stop Firebase from cancelling the function deployment.

Just before this problem i accidentally had two different cloud functions in my index.ts with the same name and deployed them. I changed the names upon noticing, removed them from within the firebase console and re-deployed. I don’t know if this could have anything to do with my problem but just wanted to mention it in case.

>Solution :

With the Admin SDK version 8 you cannot use the modular syntax and you need to use the global admin namespace.

Therefore
const teachersRef = db.collection(db, 'schools/' + 'y70B7KSbwd2D55SRPItY' + "/teachers");

must be changed to

const teachersRef = admin.firestore().collection('schools/' + 'y70B7KSbwd2D55SRPItY' + "/teachers");


If you want to use the modular syntax, you’ll need to upgrade your Admin SDK version to version 10 and change the way you import it, as explained here in the doc.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading