0

I am trying to write a Cloud Function that validates a user's email when a new user is added to the users Firestore table. (See this documentation.) I have this TypeScript code:

exports.validateEmail = functions.firestore
.document('users/{userId}')
.onCreate(async (snapshot, context) => {
    const email = snapshot.get('email');
    console.log(email);  // Correctly logs email
    await validateEmail(email);
});

export const validateEmail = async (email: string) => {
    console.log(`Validating email: ${email}.`);  // Code doesn't get to this line
    // more code
};

However, I am getting the error:

"Cannot read property 'eventType' of undefined"

There is a similar question here although it refers to 'match' of undefined and none of the proposed solutions worked/applied for me.

I set the Cloud Function up by running firebase init in a new project directory, selecting my Firebase project, choosing Functions, opting to install dependencies with npm, and opting for TypeScript. Here are relevant parts of my package.json:

"engines": {
    "node": "10"
},
"main": "lib/index.js",
"dependencies": {
    "firebase-admin": "^9.2.0",
    "firebase-functions": "^3.11.0",
},
"devDependencies": {
    "firebase-functions-test": "^0.2.0",
    "tslint": "^5.12.0",
    "typescript": "^3.8.0"
}

And here is part of my tsconfig.json:

"compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
}

Does anyone know what is undefined here - i.e. what is eventType a property of, and how can I fix it?

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
Lauren
  • 15
  • 5
  • Please edit the question to be clear about what you've done to set this up, including your package.json and any changes you might have made to it. There should be enough information in your question so that anyone can reproduce the issue. – Doug Stevenson Oct 01 '20 at 15:25
  • I think your problem is [this](https://stackoverflow.com/questions/44444273/cloud-functions-for-firebase-async-await-style) and you are not changing your async/wait code in transpilation to javascipt – JTejedor Oct 01 '20 at 15:53
  • @JTejedor, I do see that in the index.js the async/await is still present after transpilation with tsc, but I thought that async/await is supported in Node 10? – Lauren Oct 02 '20 at 17:35
  • Did you read the link that I provide you?? That question explicitly explains why you can not use async/wait in firebase functions, and it is related to the way firebase works... – JTejedor Oct 02 '20 at 18:54
  • @JTejedor, I did read the link. It says that at the time of the question, Firebase used Node 6, which doesn't support async/await. However, my Firebase project is using Node 10, which does support those keywords. And indeed, once I resolved the issue by renaming the function, it ran fine. – Lauren Oct 05 '20 at 12:18
  • Yeah, my mistake. Apologies. I am happy you finally know what is your problem. – JTejedor Oct 05 '20 at 16:10
  • @JTejedor Thanks for trying to help! Turned out it was just something very silly. – Lauren Oct 06 '20 at 20:14

1 Answers1

1

You are redefining the validateEmail function. You should just give the utility function another name that doesn't conflict with name of the first exported Cloud Function.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441