如何使用angularfire检查firebase中是否存在数据? [英] How to check if data exists in firebase using angularfire?

查看:39
本文介绍了如何使用angularfire检查firebase中是否存在数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 firebase 中手动注册用户.我如何检查用户是否已经注册或者他的( USERID )是否存在?如果存在,则不应让他注册,否则,如果他的用户 ID 尚未在数据库中,则应保存他的信息.这是我当前的代码,其中仅保存用户信息仍然可用.

I would like to register users manually in firebase. How can i check if the user is registered already or if his ( USERID ) exists? If it exists it should not let him register otherwise if his userid is not yet on the database then his info should be saved. Here is my current code wherein only saving userinfo is still available.

$scope.details={};

$scope.registerme= function() {
var someDate = new Date();
var ref = firebase.database().ref('/Users/');
$scope.submitme = $firebaseArray(ref);


            $scope.submitme.$add({
            facebookid: $scope.details.userid,
            firstname: $scope.details.firstname,
            lastname: $scope.details.lastname,
            timestamp: someDate.toString(),
            }).then(function(ref) {
            alert('Registration success.');
            }).catch(function(error) {
            alert('Registration Failed.');
            });

};

推荐答案

AngularFire 没有内置任何内容来检测节点是否存在.但由于 AngularFire 建立在 Firebase JavaScript SDK 之上,因此您可以使用 JavaScript API 做到这一点:

There is nothing built into AngularFire for detecting if a node exists. But since AngularFire is built on top of the Firebase JavaScript SDK, you can do this with the JavaScript API:

ref.child(uid).once('value', function(snapshot) {
    console.log(snapshot.exists());
});

需要注意的重要一点是,此代码段使用了 value 事件,如果当前位置没有数据,它将触发 null.

The important thing to realize is that this snippet uses a value event, which will fire null if there is no data at the current location.

另一方面,来自 AngularFire 的 $firebaseArray() 使用 Firebase 的 child_* 事件,该事件不能用于检测集合中特定子项的存在.

A $firebaseArray() from AngularFire on the other hand uses Firebase's child_* events, which cannot be used to detect existence of a specific child in the collection.

这篇关于如何使用angularfire检查firebase中是否存在数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆