嘿,我想知道是否有人可以看看这段代码并告诉我为什么它没有做我想做的事情。
如果您在catch函数中查看错误处理发生的情况,我希望它将页面路由到某个位置,错误处理中的条件似乎正在起作用。
我测试了它,如果我提交的数据库不存在于数据库中,它会抛出我编程的警报。此外,如果用户确实存在且密码错误,则会抛出第二个警报。但是,如果电子邮件是正确的并且密码正确 - 我假设它已登录,但它没有启动while循环来路由页面。
我假设这可能是因为这个区域是为了处理错误而没有发生错误我不能用它来这样做。或者这个捕获函数的范围是这样的,我不能从该区域内分配窗口位置?我想知道我做错了什么,如果有另一种方法可以做到 - 我不能把循环放在外面因为它会同时运行然后即使有错误它仍然会路由我不想要的页面即将发生。
由于
<script type="text/javascript">
function submitform() {
var email_input = document.getElementById("myForm").elements.namedItem("email").value;
var password_input = document.getElementById("myForm").elements.namedItem("password").value;
var adminID = document.getElementById("myForm").elements.namedItem("uniqueID").value;
if ((email_input.indexOf('.edu') >= 0) && (password_input.length > 4)) {
firebase.auth().signInWithEmailAndPassword(email_input, password_input).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
var email_input = document.getElementById("myForm").elements.namedItem("email").value;
var password_input = document.getElementById("myForm").elements.namedItem("password").value;
var adminID = document.getElementById("myForm").elements.namedItem("uniqueID").value;
var schools = ["@fordham.edu", "@sjsu.edu"];
var mapLocations = ["#select-a-machine", "#select-a-machine"];
var adminIdLocation = ["#select-a-machine"]
var adminIDs = ["GJGBING"]
var i = 0;
var a = 0;
if(errorCode === "auth/user-not-found") {
alert("User does not exist - go ahead and sign up first");
return;
} else if(errorCode === "auth/wrong-password") {
alert("You might have the wrong password, try again");
return;
} else {
while (a < adminIDs.length) {
if (adminID.indexOf(adminIDs[a]) >= 0) {
window.location.assign(adminIdLocation[a]);
break;
} else if (adminID.indexOf(adminIDs[a]) < 0) {
a++;
}
};
while (i < schools.length) {
if(email_input.indexOf(schools[i]) >= 0) {
window.location.assign(mapLocations[i]);
break;
} else if(email_input.indexOf(schools[i]) < 0) {
i++;
}
};
if(i >= schools.length && (email_input.indexOf('.edu') >= 0)) {
alert("We are currently not at your school");
};
}
// ...
});
} else if ((adminID.indexOf('GJGBING') >= 0) && (password_input.length > 4)) {
firebase.auth().signInWithEmailAndPassword(email_input, password_input).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
var email_input = document.getElementById("myForm").elements.namedItem("email").value;
var password_input = document.getElementById("myForm").elements.namedItem("password").value;
var adminID = document.getElementById("myForm").elements.namedItem("uniqueID").value;
var schools = ["@fordham.edu", "@sjsu.edu"];
var mapLocations = ["#select-a-machine", "#select-a-machine"];
var adminIdLocation = ["#select-a-machine"]
var adminIDs = ["GJGBING"]
var i = 0;
var a = 0;
if(errorCode === "auth/user-not-found") {
alert("User does not exist - go ahead and sign up first");
return;
} else if(errorCode === "auth/wrong-password") {
alert("You might have the wrong password, try again");
return;
} else {
while (a < adminIDs.length) {
if (adminID.indexOf(adminIDs[a]) >= 0) {
window.location.assign(adminIdLocation[a]);
break;
} else if (adminID.indexOf(adminIDs[a]) < 0) {
a++;
}
};
while (i < schools.length) {
if(email_input.indexOf(schools[i]) >= 0) {
window.location.assign(mapLocations[i]);
break;
} else if(email_input.indexOf(schools[i]) < 0) {
i++;
}
};
if(i >= schools.length && (email_input.indexOf('.edu') >= 0)) {
alert("We are currently not at your school");
};
}
// ...
});
} else {
alert("Please use a .edu email address or enter your unique Admin ID");
return;
};
};
</script>