Reputation: 1583
When I run this script from the command line (node script.js
), it seems to work fine. All the activities from Firestore get printed to the console, followed by "done.". But then the script doesn't exit right away. It hangs for exactly 60 seconds, and then I get my prompt back. What's up with that?
Update: Yes, this is the whole script.
const firebase = require('firebase')
const config = {
apiKey: 'AIzaSyBmauMItX-bkUO1GGO_Nvrycy1Y6Pj1o_s',
authDomain: 'fir-test-app-501b1.firebaseapp.com',
databaseURL: 'https://fir-test-app-501b1.firebaseio.com',
projectId: 'fir-test-app-501b1',
}
firebase.initializeApp(config)
firebase.firestore().collection('activities')
.get()
.then(qs => qs.docs.forEach(doc => console.log(doc.data())))
.then(() => console.log('done.'))
I am using Node v8.11.3 and Firebase v5.11.1. GitHub link: https://github.com/danbockapps/firebase60
Upvotes: 1
Views: 595
Reputation: 1104
Here you go:
var firebaseApp = firebase.initializeApp(config);
var database = firebase.firestore();
database.collection('activities')
.get()
.then(qs => qs.docs.forEach(doc => console.log(doc.data())))
.then(() => {
console.log('done.');
firebaseApp.delete();
// database.disableNetwork(); // Another way to do this, though not as clean
});
Managed to dig into the code of the .get() call on the collection and see that there is a 60 second timeout in AsyncQueue that lives after everything is done. Figured there must be a disconnect switch somewhere that shortcircuits the inner timers that that google library runs on and found it in firebaseApp.delete() which does the following according to its docs:
(method) firebase.app.App.delete(): Promise
Renders this app unusable and frees the resources of all associated services.
This works for 5.11.1 as well as 6.0.4, I highly recommend upgrading now while you're early in the project. I was using the npm package firebase-admin instead of firebase like you're using and did NOT need to make this call to free resources.
Upvotes: 2