Firebase-tools: In emulators, realtime database, firestore write (via emulator functions) is showing success but can't see data in the firebase console

Created on 13 Sep 2019  路  10Comments  路  Source: firebase/firebase-tools

[REQUIRED] Environment info


firebase-tools: 7.3.2


Platform: Ubuntu

[REQUIRED] Test case

In emulator, I'm writing to realtime database and firestore, via emulator functions. The write shows success, but I can't see it's written to db (firestore, rtdb) in firebase console.

[REQUIRED] Steps to reproduce

Cloud functions:

exports.singleTask = functions.https.onRequest((req, res) => {

    return admin.database().ref(`admin/singleTask/done`)
    .transaction((current_value) => {
        console.log(current_value);
        if (current_value === null || current_value === 0) {
            return (current_value || 0) + 1;
        } else {
            //eslint-disable-next-line consistent-return
            return; // Abort the transaction.
        }

    })
    .then((trans) => {
        if (trans.committed) {

            return firestore.doc('col/doc1').set({'key': 'val'});

        } else {
            return Promise.reject(new Error('duplicate'));
        }
    })
    .then(_commit => {
        return res.send('updated');
    })
    .catch((err) => {
        if (err.message === 'duplicate') {
            return res.send('duplicate')
        }else{
            console.error(err);
            return res.send('error')
        }
    })
});

[REQUIRED] Expected behavior

In firebase console, RTDB, admin/singleTask/done should be set to 1
&&
in firestore, col/doc1 should contain key: "val"

[REQUIRED] Actual behavior

Write is successfull in cloudfunctions, but not showing in console.

Most helpful comment

@hkchakladar for Firestore there's no easy way to view data in your browser, we're working on it!

If you want to write back to production, you should not run the DB emulators. So firebase emulators:start --only functions. Then any code that runs in the function will talk to production. Of course then you can't trigger the functions based on local DB events but that would be a strange combination anyway.

All 10 comments

This issue does not seem to follow the issue template. Make sure you provide all the required information.

@hkchakladar that's the intended behavior because it is writing back to the Database Emulator, which can't be seen by the console.

You can view the data by going to:
http://localhost:PORT/admin/singleTask/done.json?ns=YOUR_PROJECT_ID

Make sure to replace PORT with the port the RTDB emulator is using and YOUR_PROJECT_ID with your Firebase project ID.

Okay.

What about firestore?

And is there any way to write back to production database?

@hkchakladar for Firestore there's no easy way to view data in your browser, we're working on it!

If you want to write back to production, you should not run the DB emulators. So firebase emulators:start --only functions. Then any code that runs in the function will talk to production. Of course then you can't trigger the functions based on local DB events but that would be a strange combination anyway.

Closing this issue as I believe the question is now answered.

i am hitting my head to figure out how to make my functions talk to emulated database ... all my functions are simply talking to database in the cloud. I want to debug / develop my application locally so that i can speed up the process. I have a huge list of APIs and they store/fetch the information from the realtime database. I am not sure how i can setup a local development environment with firebase cli so that i can work with the realtime database and development data locally. without even bothering on what is stored in the cloud database.

@anantanandgupta Start the emulators by using firebase emulators:start That doesn't talk to production database. I think you're starting by firebase serve.

@hkchakladar thanks it was helpful ... it is pointing to the local database now. Next is how to load the initial data (that to without triggering the db triggers (onCreate / onUpdate etc.)). If this is done ... my life is sorted ... my application i was all building on the real database all the time and it was really difficult to do something with the sample data in the live environment.

@anantanandgupta that's a known feature request and you can track it here:
https://github.com/firebase/firebase-tools/issues/1167

Thanks for letting us know!

thanks @samtstern but that thread seems to be only focusing on firestore db, is the realtime db also in the same scope?

Was this page helpful?
0 / 5 - 0 ratings