Hello Dears,
I have added another firestore database to my Firebase project, and when I want to trigger the
Hello @AmaFB,
To access a non-default Firestore database in your Cloud Function's onDocumentCreated trigger, you need to correctly initialize and reference the other database. Here’s how you can do it:
1. Initialize the other Firestore database in your function:
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: 'https://<your-not-default-database-id>.firebaseio.com'
});
const otherDb = admin.firestore();
2. Use Firestore triggers correctly. Unfortunately, Firestore triggers like onDocumentCreated can only listen to the default Firestore instance . directly. However, you can still use onDocumentCreated to detect changes in the default Firestore and then manually read or write to the other database.
Here’s an example:
exports.onSurveyCreated = functions.firestore
.document('surveys/{surveyId}')
.onCreate(async (snap, context) => {
const newSurvey = snap.data();
const surveyId = context.params.surveyId;
// Now write to the other Firestore database
await otherDb.collection('surveys').doc(surveyId).set(newSurvey);
console.log('Survey written to other database');
});
By doing this, you listen to the default Firestore instance and then manually perform operations on your non-default Firestore instance.
Hope this helps!
If you have any doubt feel free to ask.
Hello catherinwilliam ;
Many thanks for your response,
But I found this: https://firebase.google.com/docs/reference/functions/2nd-gen/node/firebase-functions.firestore.docum...
which means you can define it like this:
Hope this is true!
When I ran the above code I got :
ERROR 2024-06-09T05:02:46.843235Z Error: 3 INVALID_ARGUMENT: The request was for database 'projects/project-id/databases/(default)' but was attempting to access database 'projects/project-id/databases/otherDb' at callErrorFromStatus (/workspace/node_modules/@grpc/grpc-js/build/src/call.js:31:19)
Hey @AmaFB
To solve the issue of triggering a Firestore function on a document creation in a non-default Firestore database, here is a step-by-step, human-friendly guide:
First, you need to initialize the Firestore database you want to access. This is done by setting up Firebase Admin SDK to point to your non-default Firestore database:
const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: 'https://<your-not-default-database-id>.firebaseio.com'
});
const otherDb = admin.firestore();
Firestore triggers like onDocumentCreated can only listen to events on the default Firestore database. You can use these triggers to detect changes and then manually perform operations on the non-default database.
Here’s how you can set up a trigger to listen to document creations in the default Firestore database and then copy the data to the other Firestore database:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const otherDb = admin.firestore();
exports.onSurveyCreated = functions.firestore
.document('surveys/{surveyId}')
.onCreate(async (snap, context) => {
const newSurvey = snap.data();
const surveyId = context.params.surveyId;
// Write to the other Firestore database
await otherDb.collection('surveys').doc(surveyId).set(newSurvey);
console.log('Survey written to other database');
});
As of now, Firestore triggers do not support direct operations on non-default databases. The approach is to use the default database to detect changes and then perform the required operations on the other database manually, as shown above.
You mentioned finding documentation suggesting you can specify a different database directly in the trigger. However, if you're encountering errors like:
As of now, Firestore triggers do not support direct operations on non-default databases. The approach is to use the default database to detect changes and then perform the required operations on the other database manually, as shown above.
You mentioned finding documentation suggesting you can specify a different database directly in the trigger. However, if you're encountering errors like:
Error: 3 INVALID_ARGUMENT: The request was for database 'projects/project-id/databases/(default)' but was attempting to access database 'projects/project-id/databases/otherDb'
This means the feature might not be fully supported or there could be a misconfiguration. Stick to using the default database for triggers and then manually interacting with other databases.
By following these steps, you should be able to achieve the desired functionality of accessing and manipulating data in a non-default Firestore database.
If you have any query then feel free to connect with me.
Hello @catherinwilliam,
Thank you so much for your response! 😊
In my case, I added another non-default database and set it up to act as my default. This means that whenever I create a document, it gets created in the non-default database.
Due to this setup, the trigger from the default database cannot be activated.
From your explanation, I understand that it is not possible to trace the onDocumentCreated() trigger in a non-default database.
Kind Regards,
Amro
Hi @catherinwilliam ,
I think what you said here "Firestore triggers like onDocumentCreated can only listen to events on the default Firestore database." is not right, I actually managed to make a cloud function react to a document creation in a secondary database in the same project... the structure of my project is this one:
@andrescoppo
Hi Andre. Solution here: GitHub - Accessing Multiple Firestore Databases and Service Accounts In The Admin SDK
We create a friendly function of the Firebase Admin SDK that allows us to freely navigate between databases and service accounts. Since the imports are modular, the global namespace allows us to freely access the object that is our various apps and databases.
EXAMPLES:
var ProfileDoc = await FDB({db:'abc',path:${path}/profiles/account}).get()
var AuthCollection = FDB({db:'xyz',path:'firewall'})
var Query = await FDB({}) .where('userId','==',userId) .get()
Note the FDB is synchronous, so the await applies to the get(), since we have a document or collection reference. Note that passing no parameters to FDB returns the default firestore.
Thanks for your answering
Example of a working solution updating multiple databases within the same project: https://gist.github.com/mtio/487d300aa4da54488ef90628ea516898