英文:
While Testing Firebase httpsCallable functions Firebase Client giving "FirebaseError: internal" Error
问题
重现步骤:
- 运行
firebase init
初始化 Firebase 可配置。然后选择Functions
和Emulators
。 - 对于 Emulators,选择 Functions 模拟器和 UI 模拟器。然后为 Functions 模拟器分配端口
5001
,为 UI 分配端口2000
。 - 对于 Functions,选择 TypeScript 并让 Firebase 下载依赖项。然后将以下代码粘贴到
functions/index.ts
中:
import * as functions from "firebase-functions";
export const helloWorld = functions.https.onCall((data, context) => {
console.log(data);
return { success : true };
});
- 在
functions/
文件夹内运行npm run build
来构建函数。然后使用firebase emulators: start
启动 Firebase 模拟器。 - 现在通过安装 Firebase SDK 并使用以下方式将 SDK 初始化为 firebaseConfig 来初始化新项目 index.ts:
import { initializeApp } from "firebase/app";
import { connectFunctionsEmulator, getFunctions, httpsCallable } from "firebase/functions";
const firebaseConfig = { /*...firebaseConfig*/ };
const app = initializeApp(firebaseConfig);
const functions = getFunctions(app);
const useEmulator:boolean = true
if(useEmulator) {
connectFunctionsEmulator(functions, "localhost", 5001);
}
const triggerCallable = httpsCallable(functions, "helloWorld");
const result = await triggerCallable({
message : "Hello world",
count: 1
});
console.log(result); // 期望结果: { success : true }
- 使用
npm run build && node dist/index.js
运行index.ts
文件,你会得到:
node:internal/process/esm_loader:97
internalBinding('errors').triggerUncaughtException(
^
[FirebaseError: internal] {
code: 'functions/internal',
customData: undefined,
details: undefined
}
我甚至在模拟器日志中都没有看到函数初始化的消息。尽管我确实收到
✔ functions: Loaded functions definitions from source: helloWorld.
看起来函数甚至都没有正确初始化。我也尝试了使用 onCall()
第二代 Firebase 函数,结果也是一样的 FirebaseError: internal
。
资源:
firebase.json:
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
],
"emulators": {
"functions": {
"port": 5001
},
"ui": {
"enabled": true,
"port": 2000
},
"singleProjectMode": true
}
}
项目结构:
.
├── database-debug.log
├── dist
│ ├── index.js
│ └── index.js.map
├── firebase-debug.log
├── firebase.json
├── functions
│ ├── lib
│ │ ├── index.js
│ │ └── index.js.map
│ ├── package.json
│ ├── package-lock.json
│ ├── src
│ │ └── index.ts
│ └── tsconfig.json
├── package.json
├── package-lock.json
├── pubsub-debug.log
├── src
│ └── index.ts
├── tsconfig.json
└── ui-debug.log
我的客户端应用的 tsconfig.json
如下:
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2020",
"sourceMap": true,
"outDir": "dist"
},
"include": ["src/**/*"],
}
我已经尝试了一切,请有人帮助:)
提前感谢。
英文:
Steps to Reproduce :
- Run
firebase init
to initialize the Firebase Configurable. Then selectFunctions
andEmulators
. - For Emulators select Functions emulator and ui emulator. Then for Functions emulator assign port as
5001
and for ui assign port as2000
. - For Functions select typescript and let firebase download the dependencies. Then paste the following code in
functions/index.ts
:
import * as functions from "firebase-functions";
export const helloWorld = functions.https.onCall((data, context) => {
console.log(data);
return { success : true };
});
- Run
npm run build
inside thefunctions/
folder to build the function. Then start the firebase emulator usingfirebase emulators: start
- Now initialize a new project by installing firebase sdk and initializing the SDK with firebaseConfig as follows index.ts:
import { initializeApp } from "firebase/app";
import { connectFunctionsEmulator, getFunctions, httpsCallable } from "firebase/functions";
const firebaseConfig = { /*...firebaseConfig*/ };
const app = initializeApp(firebaseConfig);
const functions = getFunctions(app);
const useEmulator:boolean = true
if(useEmulator) {
connectFunctionsEmulator(functions, "localhost", 5001);
}
const triggerCallable = httpsCallable(functions, "helloWorld");
const result = await triggerCallable({
message : "Hello world",
count: 1
});
console.log(result); // expected : { success : true }
- Run the
index.ts
file withnpm run build && node dist/index.js
, you will get :
node:internal/process/esm_loader:97
internalBinding('errors').triggerUncaughtException(
^
[FirebaseError: internal] {
code: 'functions/internal',
customData: undefined,
details: undefined
}
> I am not even getting the function intialized message in the emulator logs. Although I do get ✔ functions: Loaded functions definitions from source: helloWorld.
It looks like the function is not even initializing it correctly. I have also tried with onCall()
2nd gen firebase functions and it too has the same result FirebaseError: internal
Resources :
firebase.json :
{
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}
],
"emulators": {
"functions": {
"port": 5001
},
"ui": {
"enabled": true,
"port": 2000
},
"singleProjectMode": true
}
}
Project Structure :
.
├── database-debug.log
├── dist
│ ├── index.js
│ └── index.js.map
├── firebase-debug.log
├── firebase.json
├── functions
│ ├── lib
│ │ ├── index.js
│ │ └── index.js.map
│ ├── package.json
│ ├── package-lock.json
│ ├── src
│ │ └── index.ts
│ └── tsconfig.json
├── package.json
├── package-lock.json
├── pubsub-debug.log
├── src
│ └── index.ts
├── tsconfig.json
└── ui-debug.log
Where my tsconfig.json
of the client app is :
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2020",
"sourceMap": true,
"outDir": "dist"
},
"include": ["src/**/*"],
}
I have tried everything please somebody help
Thanks in advance.
答案1
得分: 1
我认为问题可能与 https://github.com/nodejs/node/issues/40537 有关。简而言之,从 Node 17 开始,默认的 IP 解析方式发生了变化。现在默认情况下使用 IPv6 回环地址,即 ::1
。
由于看起来仿真器正在使用主机 127.0.0.1
,要解决此问题,请尝试将 localhost
更改为 127.0.0.1
。
if (useEmulator) {
connectFunctionsEmulator(functions, "127.0.0.1", 5001);
}
英文:
I think the problem might be related to https://github.com/nodejs/node/issues/40537. TLDR; starting from Node 17 there was a change to the default IP resolving. By default, it now uses the IPv6 loopback address, which is ::1
.
Since it looks like the emulator is using the host 127.0.0.1
, to resolve this try changing localhost
to 127.0.0.1
.
if (useEmulator) {
connectFunctionsEmulator(functions, "127.0.0.1", 5001);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论