【发布时间】:2021-01-17 08:26:12
【问题描述】:
我正在尝试编写一个云函数,用于在将新用户添加到 users Firestore 表时验证用户的电子邮件。 (See this documentation.) 我有这个 TypeScript 代码:
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
};
但是,我收到了错误:
“无法读取未定义的属性‘eventType’”
有一个类似的问题here 尽管它指的是未定义的“匹配”,并且没有一个提议的解决方案对我有效/适用。
我通过在新项目目录中运行 firebase init、选择我的 Firebase 项目、选择 Functions、选择使用 npm 安装依赖项并选择 TypeScript 来设置 Cloud Function。以下是我的 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"
}
这是我的 tsconfig.json 的一部分:
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
}
有谁知道这里未定义的内容 - 即 eventType 的属性是什么,我该如何解决?
【问题讨论】:
-
请编辑问题以明确您为设置它所做的工作,包括您的 package.json 以及您可能对其所做的任何更改。您的问题中应该有足够的信息,以便任何人都可以重现该问题。
-
我认为您的问题是this,并且您没有将异步/等待代码转换为 javascipt
-
@JTejedor,我确实看到 index.js 中的 async/await 在使用 tsc 编译后仍然存在,但我认为 Node 10 支持 async/await?
-
您阅读我提供给您的链接了吗?该问题明确解释了为什么您不能在 firebase 函数中使用 async/wait,并且它与 firebase 的工作方式有关......
-
@JTejedor,我确实阅读了链接。它说在提出问题时,Firebase 使用了不支持异步/等待的节点 6。但是,我的 Firebase 项目使用的是 Node 10,它确实支持这些关键字。事实上,一旦我通过重命名函数解决了这个问题,它就运行良好。
标签: javascript firebase google-cloud-firestore google-cloud-functions