Node.js App Doesn't End When Calling Firebase Database Query -


this question has answer here:

i have simple firebase query running in node.js returns data successfully. however, node.js app doesn't finish. hangs if it's waiting firebase related complete. what's correct way implement 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 exists method see if product url exists in firebase.

firebase.exists(url)     .then(data => console.log(data))     .catch(data => console.log(data)); 

method check if product url exists in firebase. returns promise. note use of firebase once method.

public exists(url): {     return this.firebase.database().ref('products/').orderbychild("url").equalto(url).once("value").then(function (snapshot) {         return snapshot.exists();     }); } 

code example https://firebase.googleblog.com/2016/01/keeping-our-promises-and-callbacks_76.html

// fetch blog post id. returns promise of actual object, not datasnapshot. function getarticlepromise(id) {   return ref.child('blogposts').child(id).once('value').then(function(snapshot) {     return snapshot.val();   }); } 

answer: able resolve using process.exit() suggested frank below.

firebase.exists(url)     .then(data => { console.log(data); process.exit(); })     .catch(data => console.log(data)); 

your returning twice function, doesn't make sense. believe want this:

public exists(url): {     this.firebase.database().ref('products/').orderbychild("url").equalto(url).once("value").then(function (snapshot) {         return snapshot.exists();     }); } 

Comments