Node.JS hangs after i do a basic set

564 views
Skip to first unread message

FireBoy

unread,
Jun 29, 2018, 7:26:50 PM6/29/18
to Firebase Google Group
This might be a dumm question but i cant find an answer in the firebase NodeJs samples or docs.
I used the sample provided and i am able to connect to Firebase Db and i can Set(insert) record 
fine but afterwards my node.JS script just sits there and i have to kill it. 

Here is the code i am using so what am i missing ?

// Get a database reference to our blog
var db = admin.database();
var ref = db.ref("server/saving-data/fireblog");
var usersRef = ref.child("users");
usersRef.set({
 alanisawesome: {
   date_of_birth: "June 23, 1912",
   full_name: "Alan Turing"
 },
 gracehop: {
   date_of_birth: "December 9, 2018",
   full_name: "Grace Hopper"
 }
});


Michael Bleigh

unread,
Jun 29, 2018, 7:52:43 PM6/29/18
to Firebase Google Group
The Firebase Node.js SDK keeps a connection to the Realtime Database open once you've first connected, so the event loop never clears. You have to manually exit the process with e.g.

return ref.set({...}).then(() => process.exit(0))

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/aaad59ff-cdda-4243-a71c-8d49a2c596e2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

FireBoy

unread,
Jun 30, 2018, 10:04:42 AM6/30/18
to Firebase Google Group
Thanks, after some digging i found that out as well. Wish they would mention that upfront in doc's / sample. The sample are great but only helpful
when they work as expected or are complete.

Achim

unread,
Jun 30, 2018, 10:47:52 PM6/30/18
to Firebase Google Group
As I struggled with this as well, here is another way of closing the connections:

Depending on what features you use (ie. auth, firebase), some combination of the following will make your node script terminate without calling process.exit():


const app = firebase.initializeApp(...);

firebase.database().ref()
.update(...)
.then(() => firebase.database().goOffline())
.then(() => firebase.auth().signOut())
.then(() => app.delete())
Reply all
Reply to author
Forward
0 new messages