我正在构建一个博客来提升我的Firebase技能,但希望能够看到数据库中是否存在具有相同slug(标题只包含字母,空格替换为连字符)的博客。这是我的数据库的结构,其中所有的slu will都属于"项目"。
这是我的代码,但无论slug是什么,它总是会返回false:
firebase.database().ref().child(projectRef + 'projects/').child(slug).once('value', function(snapshot) {
var exists = (snapshot.val() !== null);
alert(exists);
});
非常感谢任何帮助,如果有更简单的方法,我总是渴望学习!
答案 0 :(得分:1)
您可以使用DataSnapshot.hasChild来确定某个孩子是否存在。
firebase.database().ref(projectRef + 'projects/').once('value', function(snapshot) {
if (snapshot.hasChild(slug)) {
alert('exists');
}
});
注意:slug是您要检查的博客标题