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

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

问题描述

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

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());
});

要实现的重要一点是此代码段使用 event,如果当前位置没有数据,将触发 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.

A <另一方面来自AngularFire的code> $ 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天全站免登陆