Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我需要一种打破这个循环的方法或者另一种方法_Javascript_Firebase_Firebase Realtime Database - Fatal编程技术网

Javascript 我需要一种打破这个循环的方法或者另一种方法

Javascript 我需要一种打破这个循环的方法或者另一种方法,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,我是js和firebase的新手。我正在尝试使用firebase数据库进行自定义登录,方法是使用我自己的名为users的表。我使用for-each循环遍历数据。但是由于这个原因,else部分被多次执行。我需要打破这个循环,这样它就不会发生 这是我的数据:- {"users" : [ { "userId" : "1", "username" : "admin", "password" : "admin", "type" : "admin" }, { "u

我是js和firebase的新手。我正在尝试使用firebase数据库进行自定义登录,方法是使用我自己的名为users的表。我使用for-each循环遍历数据。但是由于这个原因,else部分被多次执行。我需要打破这个循环,这样它就不会发生

这是我的数据:-

{"users" : [ {
    "userId" : "1",
    "username" : "admin",
    "password" : "admin",
    "type" : "admin"
  }, {
    "userId" : "2",
    "username" : "cashier",
    "password" : "cashier",
    "type" : "cashier"  
  }]
}**
这是我写的代码:

var database=firebase.database();

function SignIn(){
var txtuser=document.getElementById('username').value;
var txtpass=document.getElementById('password').value;
var error=false;

firebase.database().ref('users').orderByKey().once('value').then(function(snapshot) {
  snapshot.forEach(function(childSnapshot) {
    var users = childSnapshot.child('username').val();
    var pass=childSnapshot.child('password').val();

    if(txtuser==users && txtpass==pass){
      var type=childSnapshot.child('type').val();       
      if(type=="admin"){
        location.href="admin.html";
      }
      else if(type=="cashier"){
        location.href="cashier.html";
      }            
    }
    else{
      error=true;            
    }       
  });
});

if(error==true)
{
  window.alert("Invalid Credentials");
  location.href="index.html";
}
}

只需检查
.forEach
中的
error
是否设置为
true
,然后使用
return
进行“中断”:

var-database=firebase.database();
函数签名(){
var txtuser=document.getElementById('username')。值;
var txtpass=document.getElementById('password')。值;
var错误=错误;
firebase.database().ref('users').orderByKey().one('value')。然后(函数(快照){
snapshot.forEach(函数(childSnapshot){
var用户,pass;
if(error){return;}//密码验证
不要使用在数据库中存储身份验证详细信息的方法,而是使用流

但是,由于您使用的是用户名而不是电子邮件,请将存储桶域附加到用户名(通常为
PROJECT\u ID.appspot.com

因此,您的
“管理员”
“出纳”
用户将成为
“admin@PROJECT_ID.appspot.com“
”cashier@PROJECT_ID.appspot.com“
。为了进行电子邮件身份验证,这些是有效的电子邮件地址,即使它们没有收件箱

然后,您可以使用整个web应用程序来管理用户对“admin.html”和“cashier.html”等页面的访问控制

注意:如果您向用户发送电子邮件,请确保忽略与
“*@PROJECT\u ID.appspot.com”匹配的电子邮件。

回答问题 警告:不要以这种方式进行身份验证。请使用上述方法

  • 密码应绝不以明文形式存储
  • 密码不应以纯文本形式存储
  • 用户应该永远不能访问任何数据库中其他用户的凭据
为了回答问题,您可以使用以下代码:

var database=firebase.database();

function SignIn(){
    var txtuser=document.getElementById('username').value;
    var txtpass=document.getElementById('password').value;

    firebase.database().ref('users').orderByChild('username').equalTo(txtuser).once('value')
        .then(function(snapshot) {
            if (!snapshot.hasChildren()) {
                throw "username not found";
            } else if (snapshot.numChildren() != 1) {
                throw "duplicate usernames";
            }

            // only one child at this point, so only called once
            snapshot.forEach(function(childSnapshot) {
                if (pass != childSnapshot.child('password').val()) {
                    throw "password mismatch";
                }

                var type=childSnapshot.child('type').val();
                if(type=="admin") {
                    location.href = "admin.html";
                } else if(type=="cashier") {
                    location.href = "cashier.html";
                } else {
                    throw "unknown user type";
                }
            })
        })
        .catch(function(error) { // catches any errors thrown by promises
            location.href = "index.html";
        });
}

在上面的代码中,Firebase查询返回的
Promise
捕获每个
throw
。您可以继续阅读。

在else部分中添加条件,如果值是最后一个值,则只设置error=true;在这个答案中,
if(error)
行将在firebase查询执行之前被命中。同样值得注意的是,如果您返回
true
,它将停止迭代(中断),如果以这种方式取消,它将返回
true
。因此您可以使用
var hasError=snapshot.forEach(…)
相反。@samthecodingman Ooops,你是对的!首先,你指的是
if(error){return}
行:D当然,最后一个
if(error)
应该放在传递给
的函数中,然后
!我会更新我的答案。谢谢你的回复,我能够完成电子邮件身份验证过程:)