I have a simple Firebase query running in Node.js that returns data successfully. However, the Node.js app doesn't finish. It hangs as if it's waiting for something Firebase related to complete. What's the correct way to implement this so that the Node.js app completes?
References:
Keeping our Promises (and Callbacks) https://firebase.googleblog.com/2016/01/keeping-our-promises-and-callbacks_76.html
Node.js Firebase Query https://firebase.google.com/docs/reference/node/firebase.database.Query
Code:
Call my exists method to see if a product url exists in Firebase.
firebase.exists(url)
.then(data => console.log(data))
.catch(data => console.log(data));
Method to check if product url exists in Firebase. Returns a promise. Note the use of the Firebase once method.
public exists(url): any {
return this.firebase.database().ref('products/').orderByChild("url").equalTo(url).once("value").then(function (snapshot) {
return snapshot.exists();
});
}
Code example from https://firebase.googleblog.com/2016/01/keeping-our-promises-and-callbacks_76.html
// Fetch a Blog Post by ID. Returns a Promise of an actual object, not a DataSnapshot.
function getArticlePromise(id) {
return ref.child('blogposts').child(id).once('value').then(function(snapshot) {
return snapshot.val();
});
}
ANSWER: Was able to resolve this by using process.exit() as suggested by Frank below.
firebase.exists(url)
.then(data => { console.log(data); process.exit(); })
.catch(data => console.log(data));