How can I delete a Firebase Cloud Function which deployed with Deploy Error: undefined?
Steps to reproduce my problem:
1) Create a new Firebase project
2) $ firebase init and setup functions
3) Paste the following code into functions/index.js
"use strict";
const functions = require('firebase-functions');
function aFunction() {
return 'reports/posts';
}
function getCorruptTrigger() {
return '/reports/posts/' + aFunction +'/createdAt'
}
exports.thisFnWillFailToDeploy = functions
.database
.ref(getCorruptTrigger())
.onWrite(event => {});
4) firebase deploy --only functions
5) The function will fail to deploy as expected.
What I tried to delete the Cloud Function:
Removing the function from the index.js and deploying does not delete the function, instead I get functions[thisFnWillFailToDeploy]: Deploy Error: undefined
Deleting the Cloud Function in the Google Cloud Console does not delete the function. (I get a toast saying that the function will be deleted, but this is not the case. In the logs there is an Error with "status":{"code":13})
Creating an empty cloud function with the name thisFnWillFailToDeploy also results in Deploy Error: undefined
That function in your project is in a bad state. Apparently, deploying a function with a malformed ref causes problems with that function that can't be reversed on your own. That function will failed to deploy from the outset (not even having a chance to delete it yet). After that, you can no longer update or delete the function. You'll have to contact support to recover the function, if that's what you need. You should still be able to update or delete other functions in that project.
I'm guessing it's because you have a typo in your function call that generated the name of the ref. You're missing parens on the call to aFunction. I imagine you meant for it to look like this:
function getCorruptTrigger() {
return '/reports/posts/' + aFunction() +'/createdAt'
}
If you deploy that in a new project instead, it should work.
Related
I recently enabled App Check for my firebase app and enforced it on both my cloud functions and database. The cloud function workflow is behaving correctly. However, I can no longer access the database from the function. A minimal version of my callable function looks something like this:
exports.myFunc = functions.https.onCall(async (data, context) => {
const adminApp = admin.initializeApp();
const ref = adminApp.database().ref('some-node');
if (context.app === undefined) {
throw new functions.https.HttpsError(
'failed-precondition',
'The function must be called from an App Check verified app.',
);
}
try {
return (await ref.orderByKey().equalTo('foo').get()).exists()
} catch(exc) {
console.error(exc);
}
});
This used to work before App Check, but now it fails and I see this in the logs:
#firebase/database: FIREBASE WARNING: Invalid appcheck token (https://my-project-default-rtdb.firebaseio.com/)
Error: Error: Client is offline.
Seems like I need to do something extra to get the admin app to pass App Check verification down to the database, but I haven't been able to find any documentation on this yet. I also tried using the app instance from functions.app.admin instead of initializing a new one, but this didn't help.
I have the latest version of the packages:
"firebase-admin": "^9.10.0"
"firebase-functions": "^3.14.1"
firebaser here
The behavior you're seeing is not how it's supposed to work, and we've been able to reproduce it. Thanks for the clear report, and sorry you encountered this.
If you (re)install the Firebase Admin SDK today, you won't be experiencing this same problem as we've fixed the problem in the #firebase/database dependency (in this PR).
If you're (still) experiencing the problem, you can check if you have the correct #firebase/database dependency by running:
npm ls #firebase/database
results look something like this:
temp-admin#1.0.0 /Users/you/repos/temp-admin
└─┬ firebase-admin#9.11.0
└── #firebase/database#0.10.8
If your #firebase/database version is lower than 0.10.8, you'll have to reinstall the Admin SDK, for example by deleting your node_modules directory and your package-lock.json file and running npm install again. This may also update other dependencies.
I know there are a lot of similar thread discussing similar issue, but I still don't find any solution because maybe my case is slightly different.
so I have this error when performing testing using Mocha in Firebase emulator
Error: Bucket name not specified or invalid. Specify a valid bucket
name via the storageBucket option when initializing the app, or
specify the bucket name explicitly when calling the getBucket()
method.
import * as admin from "firebase-admin";
export const app = admin.initializeApp();
const storage = app.storage();
const defaultBucket = storage.bucket(); // it seem error is in here
I am using cloud function, so I assume initializeApp() with empty argument is fine according to the documentation in here. but I have that error when run the test in emulator
my mocha test script is like this
export FIRESTORE_EMULATOR_HOST="localhost:8080" && export FIREBASE_AUTH_EMULATOR_HOST="localhost:9099" && mocha -r ts-node/register src/tests/cloud_function_tests --recursive --extension .test.ts --timeout 60000 --exit
and it seems that error only appears when I perform the testing, if I run those code above using emulator (without mocha testing), that error will never occured
I am using
Node 14
firebase-admin: 9.6.0
firebase-functions: 3.13.2
firebase tools: 9.10.0
I have created a group of Firebase Cloud Functions (v2) that are deployed in a region (europe-west1).
#index.ts
import * as apiV2 from './v2';
export const v2 = apiV2;
#v2.ts
export const addTextMessage = functions.region('europe-west1').onCall(
...
)
I want only to deploy the addTextMessage function.
I tried:
firebase deploy --only functions:v2-addTextMessage
# or
firebase deploy --only "functions:v2-addTextMessage(europe-west1)"
However the function is not deployed:
✔ functions: functions folder uploaded successfully
i functions: current functions in project: v2-addTextMessage(europe-west1)
⚠ functions: the following filters were specified but do not match any functions in the project: v2-addTextMessage(europe-west1)
What command should I use?
Try to replace the "-" by ".".
You should use :
firebase deploy --only functions:groupName.functionName
In your case :
firebase deploy --only functions:v2.addTextMessage
It's true that the CLI terminal log is misleading because if you've exceeded your deployment quota and the CLI detects that the name of your function is for instance v2-addTextMessage(europe-west-1), it will print a message suggesting you to use the command firebase deploy --only functions:v2-addTextMessage to deploy this function only, which doesn't work.
See the full Firebase CLI documentation here
You are using the correct command, however, you have not exported the addTextMessage function to your index.ts file, without that the deployment cannot find the funtion to deploy. You can export it by adding the following code to your index.ts:
export const v2-addTextMessage = apiV2.addTextMessage
Also, you cannot use the functions parameter and the function name as a String. So your command on this case would have to be:
firebase deploy --only functions:v2-addTextMessage
For Specifying region on deployment, as you already added to your code on the edited version of the question, you cannot do it on the FirebaseCLI command, thanks to #Doug Stevenson for pointing that out on the comment section.
Ideally, as you can see on this video, you would have to specify that in your cloud function code, before deployment by adding the following:
exports.v2-addTextMessage = functions
.region('europe-west1')
.storage.object().onFinalize((object) => { });
I'm trying to deploy the very first cloud function.
It works perfectly fine, but when I try to deploy it in terminal, it sets out warning saying that "functions is declared but it's value is never read".
Is this some common starting mistake, as I am new to this subject? Thank you.
I tried both imports , deploy erros remains same
// const functions = require('firebase-functions');
import * as functions from 'firebase-functions'
Errors message
index.ts file code here
Your code doesn't declare any Cloud Functions yet, so eslint warns you that you're importing functions but not using it.
The message will disappear when you declare a Cloud Function in your index.js/index.ts. For example, the documentation on getting started contains this example:
exports.addMessage = functions.https.onRequest((req, res) => {
const original = req.query.text;
return admin.database().ref('/messages').push({original: original}).then((snapshot) => {
return res.redirect(303, snapshot.ref.toString());
});
});
As you can see, this code uses functions in its first line. So if you add this (or any other Cloud Functions declaration) to your code, you're using functions and eslint will no longer warn you about it not being used.
The error will disappear when you finally use "functions" in a cloud function.
nevermind, you are better off using
const functions = require('firebase-functions');
when importing firebase-functions in your index.js
========
EDIT : ======
Make sure that you have correctly installed those dependencies, by running those npm commands in the right folder:
npm install firebase-functions#latest firebase-admin#latest --save
npm install -g firebase-tools
Probably solved, but for me I was expecting ~/functions/index.ts to be the file building but the firebase cli added ~/functions/src/index.ts and THAT was the file.
I use Firebase Cloud Functions in my project, and I have a plenty of them so when I run the regular firebase deploy I exceed the quota, so one option is to deploy only the function that was modified by using firebase deploy --only functions:function1 as mentioned in this web page. This method works only with functions that start with: exports.funcName but when I try to use it with a function like this:
function doesNotStartWithExports() {
// Does something.
}
It doesn't work. I use firebase deploy --only functions:doesNotStartWithExports but I get this output:
⚠ functions: the following filters were specified but do not match any functions in the project: doesNotStartWithExports
The Question: How to deploy Firebase functions that does not start with exports?
I faced a very similar error while trying to delete some deprecated functions:
firebase functions:delete mymodule-helloWorld --region us-central1
Output:
Error: The specified filters do not match any existing functions in project my-firebase-project.
Turns out that if I replace the '-' in namespaced/grouped (module) functions with '.', the error goes away. Weird.
firebase functions:delete mymodule.helloWorld --region us-central1
Output:
? You are about to delete the following Cloud Functions:
mymodule-helloWorld(us-central1)
Are you sure? Yes
i functions: deleting function mymodule-helloWorld(us-central1)...
✔ functions[mymodule-helloWorld(us-central1)]: Successful delete operation.
The solution is adapted from this github thread
I actually found the solution, and it's by deploying one of the function that starts with exports and uses the function that doesn't start with exports, for example:
function doesNotStartWithExports() {
// I want to deploy only this function but I can't
}
exports.anotherFunction = functions.https.onRequest((request, response) => {
// This functions uses the one that I want to deploy.
doesNotStartWithExports()
})
To update doesNotStartWithExports I use this command:
firebase deploy --only functions:anotherFunction.
I also found that if you have a regular function exported somewhere with the same name as the Firestore function you will get this error:
export myFunction() {
// do somethig here
}
exports.myFunction = functions.https.onCall((data, context) => {
// function
})
renaming either of them to a unique name will solve the issue.
This works:
But this does not work (extra lines preceding function)