Reputation: 271774
I am following the example listed here, except with modifications due to the API of the new firebase-tools
.
exports.clearMessages = functions.runWith({ timeoutSeconds: 540, memory: '2GB' }).https.onCall(messagesController.clearMessages)
export const clearMessages = async (data, context) => {
const uid = context.auth.uid
const path = `users/${uid}/messages`
return firebase_tools.firestore.delete('flightApp3', path, {
recursive: true,
shallow: true,
allCollections: true
}).then(result => {
console.log('delete result', result)
return result
})
}
However, when I run this , I see the following displayed in Cloud Functions log:
Unhandled error { Error
at Error.FirebaseError (/user_code/node_modules/firebase-tools/lib/error.js:9:18)
at module.exports (/user_code/node_modules/firebase-tools/lib/getProjectId.js:10:19)
at Command.module.exports (/user_code/node_modules/firebase-tools/lib/requirePermissions.js:11:21)
at /user_code/node_modules/firebase-tools/lib/command.js:154:38
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
name: 'FirebaseError',
message: 'No project active. Run with \u001b[1m--project <projectId>\u001b[22m or define an alias by\nrunning \u001b[1mfirebase use --add\u001b[22m',
children: [],
status: 500,
exit: 1,
stack: 'Error\n at Error.FirebaseError (/user_code/node_modules/firebase-tools/lib/error.js:9:18)\n at module.exports (/user_code/node_modules/firebase-tools/lib/getProjectId.js:10:19)\n at Command.module.exports (/user_code/node_modules/firebase-tools/lib/requirePermissions.js:11:21)\n at /user_code/node_modules/firebase-tools/lib/command.js:154:38\n at process._tickDomainCallback (internal/process/next_tick.js:135:7)',
original: undefined,
context: undefined }
However, I'm pretty sure I have an active project in my firebase CLI.
$ firebase use
Active Project: production (flightApp3)
Project aliases for /Users/myUser/Developer/flightApp3/cloud:
* default (flightApp3)
* production (flightApp3)
Run firebase use --add to define a new project alias.
Upvotes: 10
Views: 2596
Reputation: 1074
Docs https://firebase.google.com/docs/firestore/solutions/delete-collections
In /functions
directory install firebase-tools
"firebase-tools": "^7.16.2"
In cloud function import firebase-tools
and call delete
const firebaseTools = require("firebase-tools");
...
firebaseTools.firestore.delete(workspaceRef.path, {
project: process.env.GCLOUD_PROJECT, // required
recursive: true, // required
yes: true // required
})
There is no need for a token when calling firebase-tools
from a cloud function.
Also the link to the API https://github.com/firebase/firebase-tools/blob/v7.16.2/src/firestore/delete.js with code implementation for FirestoreDelete
seems wrong.
I'm successfully calling .delete(path, options)
but the code says .delete(project, path, options)
?
Upvotes: 0
Reputation: 76679
some options cannot be mixed ...
return firebase_tools.firestore.delete('flightApp3', path, {
// allCollections: true,
recursive: true,
yes: true
}).then(() => {
return {
path: path
};
});
that's how the path is being built up (path
and allCollections
also do not seem to make sense together): projects/${project}/databases/(default)/documents/users/${uid}/messages
getProjectId.js checks for rc.projects
(where options.project
is option --project
):
module.exports = function(options, allowNull) {
if (!options.project && !allowNull) {
var aliases = _.get(options, "rc.projects", {});
...
these rc.projects
are the projects
from the .firebaserc
file:
{
"projects": {
"default": "flightApp3"
}
}
or run firebase use default
to switch from alias production
to default
(or remove alias production
once for a test). FirestoreDelete(project, path, options)
also does not care about options.token
nor options.project
anymore (as the documentation suggests).
$ firebase firestore:delete --help
explains the command-line options:
Usage: firestore:delete [options] [path]
Delete data from Cloud Firestore.
Options:
-r, --recursive Recursive. Delete all documents and sub-collections.
Any action which would result in the deletion of child
documents will fail if this argument is not passed.
May not be passed along with --shallow.
--shallow Shallow. Delete only parent documents and ignore documents
in sub-collections. Any action which would orphan documents
will fail if this argument is not passed.
May not be passed along with --recursive.
--all-collections Delete all. Deletes the entire Firestore database,
including all collections and documents.
Any other flags or arguments will be ignored.
-y, --yes No confirmation. Otherwise, a confirmation prompt will appear.
the npm package (the output above) is at version 6.0.1
.
just found a relevant comment (but possibly obsolete):
The
token
must be set in the functions config, and can be generated at the command line by runningfirebase login:ci
.
this hints for environment configuration, so that functions.config().fb.token
has the token
:
firebase functions:config:set fb.token="THE TOKEN"
one can also obtain the projectId from process.env.FIREBASE_CONFIG.projectId
.
Upvotes: 2