5

I have two firebase projects, one for development (project-dev) and one for production (project-prod).

I initialized firebase CLI for writing and deploying cloud functions linked to project-prod. Then I ran firebase use --add and added project-dev so that I can deploy the same function to both projects; I don't want to rewrite the same function twice.

At this point, I faced the problem. Into the function, I have to write something to the realtime database, but when I deploy the function to project-dev it writes to project-prod's database.

What I want to achieve is that the function has to refer to the database of the project that it is deployed to. So that I have one function and when it is deployed to project-dev it writes to project-dev's database and when it is deployed to project-prod it writes to project-prod's database.

Is it possible to achieve that? If not, what's the way to go?

EDIT

Function code:

exports.AddOrders= functions.https.onRequest(async (req, res) => {
    async function addOrder(key, value) {
        var ref = app.database().ref("/orders");
        return ref.child(key).set(value);
    }

    var orders = req.body['orders'];
    var promises = [];
    for (var key in orders) {
        promises.push(addOrder(key, orders[key]));
    }
    Promise.all(promises).then(
        _ => {
            res.sendStatus(200);
            return null;
        }
    ).catch(err => {
        res.send(err);
    })
});

(This function works fine, the problem is that it writes on the wrong database)

3 Answers 3

14

After you add a project with firebase use --add <projektName> you need to select it with firebase use <projectName>. Then you can deploy to the selected project with firebase deploy or firebase deploy --only functions.

Sign up to request clarification or add additional context in comments.

6 Comments

Do you use any external routes to access the database or are you using the admin sdk?
I'm using the admin sdk
Can you post the code for your cloud function?
just edited the question to include the code
How and where are you triggering the cloud functions (both dev and prod)?
|
1

This is the answer for anyone wondering what the problem was.

He was initialising the admin sdk with the credentials of a service account tied to the production project. The solution was to change it to admin.initializeApp() without passing any arguments. This made Firebase use the default credentials for each project.

I know its a common mistake but again, here is the link to the corresponding documentation https://firebase.google.com/docs/admin/setup

Comments

1

I use firebase use to toggle between my projects and then conditionally initialize my firebase admin code using the following code:

var firebase = require("firebase-admin");
const { projectID } = require("firebase-functions/params");
var path = require('path');
var { serviceAccountDev, serviceAccountProd } = require(path.resolve( __dirname, "./serviceAccountKey.js"));

if (projectID.value() === "myapp-dev") {
   firebase.initializeApp({
      credential: firebase.credential.cert(serviceAccountDev),
      databaseURL: "https://myapp-dev-default-rtdb.firebaseio.com",
   });
} else {
   firebase.initializeApp({
      credential: firebase.credential.cert(serviceAccountProd),
      databaseURL: "https://myapp-default-rtdb.firebaseio.com",
   });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.