Victor Torrens
Victor Torrens

Reputation: 23

How to put a trigger to another Firebase Database from Firebase Functions?

I'm trying to trigger my database function when something is wrote to a database from an associate. I know that I need a Service Account created in the other database and the .json file that firebase gives to do the connection properly. Right now I'm giving all the permissions for getting sure than my errors don't come from this.
With what I found on the documentation and with other information on internet, this is how I login to the other database:

var adminAbi = require("firebase-admin");
var functionsAbi = require('firebase-functions');

const serviceAccount = require(`./serviceacountfile.json`);
adminAbi.initializeApp({
  credential: adminAbi.credential.cert(serviceAccount),
  databaseURL: 'https://DATABASEURL.firebaseio.com/',
},'test' );

And this is my trigger:

exports.copyDatabasess = functionsAbi.database.instance('test').ref('/messages/{user_id}/{now}').onWrite(event =>{
  if (!event.data.exists()) {
    return;
    }
    console.log('copydatabase', event.params.body);
  // Grab the current value of what was written to the Realtime Database.
  const original = event.data.val(); 
});

With this code I'm getting right now this error when I deploy:

! functions[copyDatabasess]: Deployment error. Failed to configure Firebase Realtime Database trigger: unknown error, HTTP code 401

I really can't find useful informations about this error and how to solve it. If someone knows something about this would be much appreciated.
Thanks in advice.

Upvotes: 0

Views: 556

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317457

You can't put a trigger on a database that's not in the same project as your functions. instance() only works with database shards in the same project.

Upvotes: 2

Related Questions