This repository holds code snippets used in Node.js documentation on firebase.google.com.
We love contributions! See CONTRIBUTING.md for guidelines.
Node.js snippets for firebase.google.com
License: Apache License 2.0
This repository holds code snippets used in Node.js documentation on firebase.google.com.
We love contributions! See CONTRIBUTING.md for guidelines.
After following the instructions for submitting a pull request and signing the CLA I still can not push my branch to origin
.
$ git push origin clean-up-delete-collection-example
ERROR: Permission to firebase/snippets-node.git denied to {username}.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
The documentation and examples do not mention if updating nested objects works with arrayUnion
This is the document's example:
var frankDocRef = db.collection("users").doc("frank");
frankDocRef.set({
name: "Frank",
favorites: { food: "Pizza", color: "Blue", subjects: ["recess", "math"] },
age: 12
});
// To update age and favorite color:
db.collection("users").doc("frank").update({
"age": 13,
"favorites.color": "Red"
})
does this work or not?
// does this work?
db.collection("users").doc("frank").update({
"favorites.subjects": FieldValue.arrayUnion("history")
})
This question is about the deleting collections and subcollections solution (Doc).
As the document describes, our function should get token
from environment variables and pass it to the delete function, but the token
would expire and the request would fail.
Any advice on how to keep the token
valid?
Thank you.
Node.js 8 support for the App Engine Standard environment is being deprecated, requiring you to upgrade your App Engine Standard applications to Node.js 10 by October 1, 2019. After said date, you will be unable to deploy Node.js 8 versions to your applications.
I am trying to deploy delete sample code given. I get the "Error: Functions did not deploy properly" error even though I followed all the steps in the ReadMe doc
Hi,
It is not possible to deploy a cloud function running a similar code as here: https://github.com/firebase/snippets-node/tree/master/firestore/solution-deletes
Whenever the 'firebase-tools' package is required from the cloud functions context the following error is thrown during the deployment:
Error: ENOENT: no such file or directory, mkdir '/www-data-home/.config/configstore'
However, if testing locally with the firebase function emulator the same code works without problems.
Could you please help me deploy my code?
Kind regards,
snippets-node/firestore/main/index.js
Lines 36 to 44 in 8985ddb
How can I set it in .env or system environment?
e.g.
admin.initializeApp({
credential: admin.credential.cert(process.env.SERVICE_ACCOUNT_KEY)
});
Is there a way to import specific Firestore Database Documents under an exported Collection from Firebase Storage?
After creating a JavaScript AppEngine Cron Job according to the Schedule Export documentation there does not seem to be a way to import specific Documents, only Collections.
In-Short: The ability to export a specified Document representing a user.
The Firestore database of the app is currently structured with a users Collection at the highest level, and then a Document representing the user containing meta data, saved content, etc. A daily Cron Job has been scheduled with the documentation above in order to back up all of the Firestore data including the user data in the event users' data is inadvertently corrupted during development or maliciously in an attack.
However, from the Import specific collections documentation it appears only a Collection can be imported.
Existing Data Structure
Refactoring the Firestore Database structure to add additional layers of Collections/Documents in order for each User to be represented by a unique Collection. The downside with the new data structure is you have a unnecessary additional users
document 2nd level.
New Data Structure
snippets-node/firestore/solution-aggregation/functions/index.js
Lines 11 to 37 in 9ae3a00
If it's retried, I think it's possible to get a wrong value for newNumRatings.
I tried to get firebase-tools as a module in my project but I was unable to import it anyway, and it was showing could not find firebase-tools.
I do not think this class is Exported for usage.
From what I saw on the node_module it was an abstract class
snippets-node/firestore/main/index.js
Line 5 in 7b30935
Please how can this be used in Cloud function?
snippets-node/firestore/main/index.js
Lines 526 to 535 in a2a7b67
var citiesRef = db.collection('cities');
var query = citiesRef.where('capital', '==', true).get()
.then(snapshot => {
if(snapshot.size>0)
snapshot.forEach(doc => {
console.log(doc.id, '=>', doc.data());
});
else
console.log("no documents");
})
.catch(err => {
console.log('Error getting documents', err);
});
Is it possible to import specific collections from Cloud Storage if you do not define specific collections in the export cron.yaml file, but rather export all collections?
In the sample documentation specific collections are named in order to export in the cron.yaml file.
cron:
- description: "Daily Cloud Firestore Export"
url: /cloud-firestore-export?outputUriPrefix=gs://BUCKET_NAME[/PATH]
target: cloud-firestore-admin
schedule: every 24 hours
Is it feasible to export all collections, yet still import specific collections later on if needed like so.
Example use case
Backing up all of an app's data, and then restoring the collections for a specific user.
gcloud alpha firestore import gs://BUCKET_NAME/USER/USER_NAME
Hi,
First off apologies if this is not the right place to ask this. But I am implementing the recommended firestore backup appengine cronjob in multiple projects and would like to avoid duplicating code, e.g. current setup:
appengine
├── README.md
├── gcpproject1
│ └── firestore-backup
│ ├── app.js
│ ├── app.yaml
│ ├── cron.yaml
│ ├── deploy.sh
│ └── package.json
└── gcpproject2
└── firestore-backup
├── app.js
├── app.yaml
├── cron.yaml
├── deploy.sh
└── package.json
Where only the deploy command in deploy.sh
(which targets correct project) and url
(query parameters outputUriPrefix
and collections
in particular) are the only differences in both folders.
What would be the recommended best practice to avoid duplicating the code? Symlinks to the common files?
Hello, is there a way I can define the export file name?
var citiesRef = db.collection('cities');
var westCoastCities = citiesRef.where('regions', 'array-contains',
'west_coast');
return westCoastCities.get()
.then(res => {
console.log('West Coast get: ', res);
});
rising this exception
Argument "opStr" is not a valid FieldComparison. Operator must be one of "<", "<=", "==", ">", or ">="
Make sample's region tags consistent across all the languages.
Thanks for these amazing snippets but I feel like there should be a warning about including this in any production environment since the combination of mintAdminToken
and recursiveDelete
would enable to anyone blasting an entire production database.
I suggest you create an example that isn't copy-pasteable.
Thank you very much for the snippit of code. It was exactly what I needed. What is the best way to control who can run the export endpoint?
https://<MY_PROJECT_ID>.appspot.com/cloud-firestore-export?outputUriPrefix=gs://<MY_STORAGE_LOCATION>/backups
In a perfect world I would like to restrict access to that endpoint to only the cron job. Is that possible?
My colleague has mentioned that:
It's usually necessary to add
process.on('SIGINT', () => process.exit());
to have the server shutdown cleanly when asked to. You can test by running the process in a > docker container with -it and using CTRL+C to ask it to stop.
Are there any reasons to not add this to the app engine service? If not I could make a PR.
As a side effect of #46, the current documentation syntax highlighting is not working as expected some parts get syntax highlighting but all that include let
are not, I've attached the screenshot below for reference.
Hi there! I'm getting this error
Error [FirebaseError]: Function
WriteBatch.update() requires at least 3 arguments, but was called with 2 arguments.
According to the documentation and examples therein, writeBatch.update()
needs only 2 arguments: the document reference and the data object.
My Working Environment:
Ubuntu 18.04
Node v12.4.0
npm 6.9.0
firebase 6.2.3
The module referenced in full-text-seach's index.js file does not exist.
On deploying, it gives the following error:
Error: Error parsing triggers: Cannot find module 'firebase-functions/lib/providers/datastore'
which is probably due to datastore being renamed to firestore?
Defined Firebase Cloud Storage directory gs://my-project-name.appspot.com/
to be populated with all of the collections in the my-project-name
's Firestore database when the deployed Cron job is ran on GCP with the implementation followed by the Schedule data exports documentation.
Cron job is deployed successfully, but fails to run. The logs are showing Error code 204
.
Logs
2019-01-24 11:19:47.818 PST GET 500 0 B 998 ms AppEngine-Google; (+http://code.google.com/appengine) /cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups/ 0.1.0.1 - - [24/Jan/2019:11:19:47 -0800] "GET /cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups/ HTTP/1.1" 500 - - "AppEngine-Google; (+http://code.google.com/appengine)" "cloud-firestore-admin.[my-project-name].appspot.com" ms=998 cpu_ms=482 cpm_usd=0 loading_request=1 instance=00c61b117c5fba456c9edcc5f075ccacb339b96d0bc7a8e35beb60cd3fd50a149d4c2f296b app_engine_release=1.9.71 trace_id=65811179272fc9c721d10e6cab7cdf70
{
httpRequest: {
status: 500
}
insertId: "5c4a0fd4000c78b4e510bd7a"
labels: {
clone_id: "00c61b117c5fba456c9edcc5f075ccacb339b96d0bc7a8e35beb60cd3fd50a149d4c2f296b"
}
logName: "projects/[my-project-name]/logs/appengine.googleapis.com%2Frequest_log"
operation: {
first: true
id: "5c4a0fd300ff0c7e86eb62e71c0001737e636f696e76657273652d6d656469612d73746167696e670001323031393031323474313130333331000100"
last: true
producer: "appengine.googleapis.com/request_id"
}
protoPayload: {
@type: "type.googleapis.com/google.appengine.logging.v1.RequestLog"
appEngineRelease: "1.9.71"
appId: "s~[my-project-name]"
endTime: "2019-01-24T19:19:48.816961Z"
finished: true
first: true
host: "cloud-firestore-admin.[my-project-name].appspot.com"
httpVersion: "HTTP/1.1"
instanceId: "00c61b117c5fba456c9edcc5f075ccacb339b96d0bc7a8e35beb60cd3fd50a149d4c2f296b"
instanceIndex: -1
ip: "0.1.0.1"
latency: "0.998139s"
line: [
0: {
logMessage: "This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application."
severity: "INFO"
time: "2019-01-24T19:19:48.816504Z"
}
1: {
logMessage: "A problem was encountered with the process that handled this request, causing it to exit. This is likely to cause a new process to be used for the next request to your application. (Error code 204)"
severity: "ERROR"
time: "2019-01-24T19:19:48.816507Z"
}
]
megaCycles: "482"
method: "GET"
pendingTime: "0.010711670s"
requestId: "5c4a0fd300ff0c7e86eb62e71c0001737e636f696e76657273652d6d656469612d73746167696e670001323031393031323474313130333331000100"
resource: "/cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups/"
startTime: "2019-01-24T19:19:47.818822Z"
status: 500
taskName: "6c799fe7c1a404ec22117967738fcca9"
taskQueueName: "__cron"
traceId: "65811179272fc9c721d10e6cab7cdf70"
traceSampled: true
urlMapEntry: "auto"
userAgent: "AppEngine-Google; (+http://code.google.com/appengine)"
versionId: "20190124t110331"
wasLoadingRequest: true
}
receiveTimestamp: "2019-01-24T19:19:48.824360342Z"
resource: {
labels: {
module_id: "default"
project_id: "my-project-name"
version_id: "20190124t110331"
zone: "us17"
}
type: "gae_app"
}
severity: "ERROR"
timestamp: "2019-01-24T19:19:47.818822Z"
trace: "projects/[my-project-name]/traces/65811179272fc9c721d10e6cab7cdf70"
traceSampled: true
}
2019-01-24 11:19:48.816 PST
This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application.
2019-01-24 11:19:48.816 PST
A problem was encountered with the process that handled this request, causing it to exit. This is likely to cause a new process to be used for the next request to your application. (Error code 204)
For the package.json the name
, version
, and description
attributes have been changed specific to the project.
In the cron.yaml the description
and url
attributes are customized. For url
the following has been attempted in order to export all collections:
/cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups
cron.yaml
url:
/
after directory: /cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups/
collections
query but leaving it empty: /cloud-firestore-export?outputUriPrefix=gs://[my-project-name].appspot.com/backups&collections
gcloud config configurations list
.This code gives me this error:
Function execution took 11879 ms, finished with status: 'connection error'
How can I fix this?
Code:
exports.businessUpdated = functions.firestore.document('businesses/{uid}').onCreate(event => {
// Get the note document
const note = event.data.data();
// Add an 'objectID' field which Algolia requires
note.objectID = event.params.noteId;
// Write to the algolia index
const index = client.initIndex(ALGOLIA_INDEX_NAME);
return index.saveObject(note);
});
When using the snippet I get a warning
Authenticating with `--token` is deprecated and will be removed in a future major version of `firebase-tools`. Instead, use a service account key with `GOOGLE_APPLICATION_CREDENTIALS`: https://cloud.google.com/docs/authentication/getting-started
at this line:
Is that because I am using it in a wrong way or is the snipped outdated? How would I use the described service account key in the case of this snippet? I appreciate your help, I am still new to GCP!!
Environment:
firebase-tools: 11.20.0
Node: 16
Steps to reproduce:
use the snipped as described
Expected behaviour:
Execute recursive delete of path
Actual behaviour:
Same, but with deprecation warning
snippets-node/firestore/main/index.js
Lines 162 to 169 in 5867388
The comment says that the code creates a document with the name 'DC', but the path is for a document with the name 'LA'. I believe the comment should say 'LA'.
Sorry if this has already been noticed, I'm still new to this.
When I emulate an onCreate
function in the firestore shell, it tells me snap.data
isn't a function. When I log snap
it seems to be an instance of DataSnapshot
, which has no data method according to the documentation, but it does have a val
method, which returns what I want.
Hi Samstern,
I tried to use your repository for the firestore backup using cron.yml. It is creating the bucket but writing 0 bytes.
I have a question about this line:
const url = https://firestore.googleapis.com/v1beta1/projects/${projectId}/databases/(default):exportDocuments
;
I have hardcoded like this: const url = 'https://firestore.googleapis.com/v1beta1/projects/cp-gaa-dev-dragonfly-arc/databases/(default):exportDocuments'
Please let me know what I am doing wrong.
Thanks
BC
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.