【发布时间】:2023-02-16 02:11:14
【问题描述】:
环境
火力地堡:9.8.0
节点:v16.14.2
问题
我正在尝试使用函数模拟器使用 Node.js 脚本测试可调用的 Firebase 云函数,但在我的终端中运行脚本时,我不断收到以下错误:
error [FirebaseError: internal] {
code: 'functions/internal',
customData: undefined,
details: undefined
}
即使在使用详细输出运行脚本时,我也没有获得任何更多信息。根据我对 Firebase documentation 的理解,在调用云函数并失败的服务器端存在错误。
Firebase 模拟器工作正常,可以正确响应使用 functions.https.onRequest() 编写的 HTTP 请求。
这是我的功能:
export const helloWorld = functions.https.onCall((data, context) => {
if (!context.auth) {
// Throwing an HttpsError so that the client gets the error details.
throw new functions.https.HttpsError('failed-precondition', 'The function must be called ' +
'while authenticated.');
}
console.log( "hello World" );
console.log( data );
console.log( context );
const responseVal = { retval:"Hello World from callable function." };
console.log( responseVal )
return responseVal;
});
这是我的 node.js 脚本(主要基于 Firebase guide for calling functions from your app):
import { initializeApp } from 'firebase/app';
import { getFunctions, httpsCallable, connectFunctionsEmulator } from 'firebase/functions';
const firebaseConfig = {
apiKey: "...",
authDomain: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "...",
measurementId: "..."
};
const app = initializeApp( firebaseConfig );
const functions = getFunctions( app );
connectFunctionsEmulator( functions, "http://localhost", 5001 );
const helloWorld = httpsCallable( functions, 'helloWorld' );
helloWorld( { hello: "hi" } )
.then( (result) => {
console.log( "worked" )
const data = result.data;
console.log( data.retval );
})
.catch( (error) => {
console.log( "error", error );
console.log( "code", error.code );
console.log( "message", error.message );
console.log( "details", error.details );
} )
有没有什么东西会导致我的节点脚本中的函数调用调用 onCall 函数但失败了?
当我尝试在模拟器运行时运行脚本以及在模拟器不运行的情况下运行脚本时,会出现相同的错误。这可能是初始化我的应用程序或将我的应用程序连接到 firebase 模拟器的问题,但我不确定哪里出错了。
谢谢
【问题讨论】:
标签: javascript node.js firebase