Reputation: 473
Environment
Firebase: 9.8.0
Node: v16.14.2
Issue
I am trying to test a callable Firebase cloud function using a Node.js script using the functions emulator, but I keep getting the following error when running the script in my terminal:
error [FirebaseError: internal] {
code: 'functions/internal',
customData: undefined,
details: undefined
}
I do not get any more information even when running the script using the verbose output. From my understanding of the Firebase documentation, there is something wrong server-side where the cloud function is being invoked and failing.
Firebase emulator is working fine and will respond correctly to HTTP requests written with functions.https.onRequest()
.
Here is my function:
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;
});
and here is my node.js script (based mostly on the 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 );
} )
Is there something that would cause the function call in my Node script to invoke the onCall function but have it fail?
The same error is given when I try run script with my emulator running and when I run the script without the emulator running. This may be an issue of initailizing my application or connecting my application to the firebase emulator but I am unsure where I have gone wrong.
Thanks
Upvotes: 2
Views: 1965
Reputation: 473
Specifying the timezone and removing "http://" from the connectFunctionsEmulator()
function resolved the issue.
const app = initializeApp( firebaseConfig );
const functions = getFunctions( app );
connectFunctionsEmulator( functions, "http://localhost", 5001 );
Changed to:
const app = initializeApp( firebaseConfig );
const functions = getFunctions( app, "us-central1" );
connectFunctionsEmulator( functions, "localhost", 5001 );
Upvotes: 5