Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property 'name' of undefined in Firebase Function after upgrading to Node 10

Tags:

I recently got warnings that Node 8 has been deprecated for Cloud Functions for Firebase. I updated to Node 10 but then I started getting this error even before the function is invoked and the function is not executed. On reverting back to Node 8 fixes the problem. I am not sure how to debug this problem to get more information.

TypeError: Cannot read property 'name' of undefined
    at dataConstructor (/workspace/node_modules/firebase-functions/lib/providers/database.js:137:85)
    at cloudFunctionNewSignature (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:119:34)
    at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:151:20)
    at Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:198:28)
    at process._tickCallback (internal/process/next_tick.js:68:7) 

The error logs are in the functions console log as shown below: enter image description here

I have a bunch of Cloud Functions and all of them are failing. I have posting implementation for one of them which has the least amount of code.

exports.isUserRunningMinimumVersion = functions.database
  .ref('/tasks/isUserRunningMinimumVersion/{taskId}')
  .onCreate((taskSnapshot, context) => {
    if (!taskSnapshot.exists()) {
      return
    }
    const task = taskSnapshot.val()

    invariant(
      task.hasOwnProperty('version'),
      "isUserRunningMinimumVersion task doesn't contain required fields [version]"
    )

    let promise
    if (semver.lt(task.version, GlobalConfig.minSupportedVersion)) {
      promise = ref.child('taskResults/' + context.params.taskId).set({
        status: 'success',
        result: {
          userIsRunningMinimumVersion: false,
        },
      })
    } else {
      promise = ref.child('taskResults/' + context.params.taskId).set({
        status: 'success',
        result: {
          userIsRunningMinimumVersion: true,
        },
      })
    }

    return promise.then(() => {
      return ref
        .child('/tasks/isUserRunningMinimumVersion/' + context.params.taskId)
        .remove()
    })
  })

Below is my package.json file

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "dependencies": {
    "@sentry/node": "^5.11.1",
    "aws-sdk": "^2.620.0",
    "axios": "^0.19.0",
    "child-process-promise": "^2.2.1",
    "firebase-admin": "^8.0.0",
    "firebase-functions": "^2.3.1",
    "google-libphonenumber": "^3.2.2",
    "googleapis": "^41.0.1",
    "i18n-js": "^3.2.2",
    "install": "^0.13.0",
    "invariant": "^2.2.4",
    "lodash": "^4.17.11",
    "mkdirp-promise": "^5.0.1",
    "nexmo": "^2.4.1",
    "nodemailer": "^6.2.1",
    "npm": "^6.13.6",
    "semver": "^6.1.1",
    "useragent": "^2.3.0",
    "uuid": "^3.3.3"
  },
  "engines": {
    "node": "10"
  },
  "private": true
}

Please share how to fix this problem.

like image 401
Varun Gupta Avatar asked Jul 22 '20 13:07

Varun Gupta


1 Answers

Your firebase-functions module is very old. The latest version is 3.8.0. Upgrade it:

npm install firebase-functions@latest
like image 188
Doug Stevenson Avatar answered Sep 30 '22 20:09

Doug Stevenson