Firebase Functions - why do I still receive unhandled error message after 'response received from function'

186 Views Asked by At

I thought that once the response was received from a cloud function that meant that the function terminated. How and why would I still receive 'Your function was killed because it raised an unhandled error.' after the response has been sent?

Response.send is called, but the function still throws an error after. This is happening in the emulator.

RESPONSE RECEIVED FROM FUNCTION: 200, Got Current Logs
⚠  functions: Your function timed out after ~60s. To configure this timeout, see
      https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
⚠  Your function was killed because it raised an unhandled error.

Also, the error doesn't take 60 seconds so I don't know why it would say that it timed out after 60 seconds.

Here is the code.

The first part is just getting an array of urls. It then calls the getLogs function. This is where I am eventually making a request and doing some other logic, but I've deleted that for now just to simulate a resolved promise.

I'm using bluebird promises for the promise.map syntax and here is the documentation for that http://bluebirdjs.com/docs/api/promise.map.html

import * as functions from 'firebase-functions'


export const handler = (database: any, urlDate: any) => {
  const scheduleRef = database.collection('NBASchedule')

  return functions.https.onRequest(async (req, response) => {
    const Promise = require('bluebird')
    const urlList: any = []

   scheduleRef.get().then((snapshot:any) => {

    snapshot.docs.forEach((doc: any) => {
      const scheduleGame = doc.data()
      if (scheduleGame.urlString.split('_')[1] === urlDate) {
        const boxScoreUrl = scheduleGame.boxScoreURL
        urlList.push('https://' + boxScoreUrl + '/')
      }
    })

    getLogs()
   }).catch((err:any) => {
    console.log(err)
   })

    async function getLogs() {

      //Promises returned by the bluebird mapper function are awaited for and the returned promise doesn't fulfill until all mapped promises have fulfilled as well.

      await Promise.map(urlList, (url: any) => {
        return new Promise(
          (resolve: any, reject: any) => {
 
            setTimeout(function () {
              resolve(1 + 1)
            }, 500)
          },
          {
            concurrency: 1,
          }
        ).catch(async (err: any) => {
          console.log(err)
        })
      })
      response.send('Got Current Logs')
    }
  })
}

Can anyone explain why I still get 'Your function was killed because it raised an unhandled error.'?

0

There are 0 best solutions below