Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close Database Connection (Firebase+Node.js)

I am currently working on Node.js and firebase and I am currently stuck in an issue, where after writing the data in database my program does not end on a terminal, is there a way I can end this.

I guess, maybe I need to end the firebase connection but I don't have an idea how would I do that.

Here is the program,

module.exports=
{
create_firebase_entry: function ()
  {
    console.log("Firebase Entry")
    return new Promise((resolve, reject) => {
      var secondaryAppConfig = 
      {
	credential: admin.credential.cert(serviceAccount),
	databaseURL: "firebase_databse_url"
      };
      
      if (admin.apps.length===0){secondary = admin.initializeApp(secondaryAppConfig, "secondary");}

      // Retrieve the database.
      var db = secondary.database();
      var ref = db.ref("data_records");
      var usersRef = ref.child("My_Data");
      
      usersRef.set({Data1:"Trail Data"})
    })
  }
}
like image 277
Himesh Avatar asked May 01 '26 04:05

Himesh


1 Answers

The script opens a connection to the Firebase Realtime Database, and then performs asynchronous read and write operations over that connection. Since the script has no way to know when you're done, you will have to signal this yourself.

The most common way to do this (that I know of) is to call process.exit() when you're done. E.g.

usersRef.set({Data1:"Trail Data"})
  .then(function() {
    process.exit(0);
  });

Also see

  • How to properly exit firebase-admin nodejs script when all transaction is completed
  • Exit from a NodeJS script when all asynchronous tasks are done
  • node process doesn't exit after firebase once
like image 169
Frank van Puffelen Avatar answered May 03 '26 10:05

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!