这个问题在这里已有答案:
我在Node.js中运行了一个简单的Firebase查询,可以成功返回数据 . 但是,Node.js应用程序无法完成 . 它挂起就好像它正在等待Firebase相关的内容完成 . 实现这个的正确方法是什么,以便Node.js应用程序完成?
参考文献:
保持我们的承诺(和回调)https://firebase.googleblog.com/2016/01/keeping-our-promises-and-callbacks_76.html
Node.js Firebase查询https://firebase.google.com/docs/reference/node/firebase.database.Query
码:
调用我的exists方法,查看Firebase中是否存在产品网址 .
firebase.exists(url)
.then(data => console.log(data))
.catch(data => console.log(data));
检查Firebase中是否存在产品网址的方法 . 返回一个promise . 请注意使用Firebase一次方法 .
public exists(url): any {
return this.firebase.database().ref('products/').orderByChild("url").equalTo(url).once("value").then(function (snapshot) {
return snapshot.exists();
});
}
代码示例来自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();
});
}
答案:能够通过使用下面的Frank建议的process.exit()来解决这个问题 .
firebase.exists(url)
.then(data => { console.log(data); process.exit(); })
.catch(data => console.log(data));
1 回答
你从函数返回两次,这是没有意义的 . 我相信你想要这个: