我正在使用 typescript/firebase/firestore 设置 NextJS 应用程序,并模拟函数、firestore 和身份验证以进行开发。我有一个 firebase 函数,可以在创建用户时随时运行。
firebase/functions/index.ts
:
import { firestore } from "firebase-admin";
import { initializeApp } from "firebase-admin/app";
import { getAuth } from "firebase-admin/auth";
import { auth, config } from "firebase-functions";
initializeApp(config().firebase);
export const onUserCreate = auth.user().onCreate(async (user) => {
if (user.email && user.email === "[email protected]") {
await firestore().doc(`users/${user.uid}`).create({
isCoordinator: true,
});
const customClaims = {
role: "coordinator",
};
try {
await getAuth().setCustomUserClaims(user.uid, customClaims);
} catch (error) {
console.log(error);
}
return;
}
if (user.email && user.email === "[email protected]") {
const customClaims = {
role: "recruiter",
};
try {
await getAuth().setCustomUserClaims(user.uid, customClaims);
} catch (error) {
console.log(error);
}
return;
}
await firestore().doc(`users/${user.uid}`).create({
isRecruiter: false,
});
});
firebase/functions/package.json
{
"name": "functions",
"scripts": {
"lint": "eslint --ext .js,.ts .",
"build": "tsc",
"build:watch": "tsc --watch",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "20"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^12.0.0",
"firebase-functions": "^4.6.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"eslint": "^8.9.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.25.4",
"firebase-functions-test": "^3.1.0",
"typescript": "^4.9.0"
},
"private": true
}
但是,运行时
firebase emulators:start
我收到以下错误:
!! functions: Failed to load function definition from source: FirebaseError: User code failed to load. Cannot determine backend specification
类似的问题已经在他们的 GitHub 上的 this issues 下进行了讨论,但没有任何实际结果。我已经降级了(正如许多人在问题讨论中建议的那样),但这没有任何帮助。
特别奇怪的是,它偶尔会起作用,我可以成功地使用云功能来做我想做的事情。有人知道我能做些什么来解决这个问题吗?
尝试贬值它,它对我有用 npm install firebase-functions@^4.4.0 firebase-admin@^11.10.0