3

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"})
    })
  }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Himesh
  • 31
  • 6

2 Answers2

3

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

I agree to Frank but I think using process.exit(0); under .finally() block will provide more guarantee. Because when error occur It will never close.

usersRef.set({Data1:"Trail Data"})
  .then(result => {
    // ...
  })
  .catch(err => {
    // ...
  })
  .finally(() => {
    process.exit(0)
  })
Serhan C.
  • 1,152
  • 1
  • 12
  • 10