我使用indexeddb来运行登录应用程序。我试图在用户输入数据库中尚未存在的用户名时运行警报。但是,onerror事件未运行我设置的警报。与onsuccess相同的功能正在工作,所以我不知道问题是什么。
登录验证:
function getLogin(){
alert('getLogin launched');
var user = logUser.value;
var pass = logPass.value;
alert('User: '+user+' Pass: '+pass+" sent to loginCheck");
loginCheck(user,pass);
alert ('user/pass checked');
}
function loginCheck(user,pass){
db.transaction("users").objectStore("users").get(user).onerror = function(event) {
alert('Sorry this is not a valid username');
};
db.transaction("users").objectStore("users").get(user).onsuccess = function(event) {
var x = event.target.result;
if(pass !== x.pw ){alert('Sorry, wrong password');
}
};
}
DB材料:
//-------------USER DB------------------//
function startDB(){
//sign in page elements
logUser = document.getElementById('logUserName');
logPass = document.getElementById('logPass');
signin = document.getElementById('signin');
signin.addEventListener('click',getLogin);
//register page elements
mainForm = document.getElementById('mainFormSidebar');
mainForm.addEventListener('submit',addObject);
fname = document.getElementById('fName');
lName = document.getElementById('lName');
users = document.getElementById('uName');
pass = document.getElementById('password');
email = document.getElementById('email');
dob = document.getElementById('dob');
phone = document.getElementById('phone');
bio = document.getElementById('bio');
terms = document.getElementById('terms');
school = document.getElementById('school');
gender = document.getElementsByName('gender');
save = document.getElementById('save');
reset = document.getElementById('reset');
reset.addEventListener('click',clearForm);
databox = document.getElementById('databox');
//open DB
var request = indexedDB.open('macroPlay');
//if fails
request.addEventListener('error', showerror);
//if succeeds
request.addEventListener('success', start);
//if !exist, create.
request.addEventListener('upgradeneeded', createdb);
//Create Admin account on launch
}
function showerror(e){
alert('Error: ' + e.code + ' - ' + e.message);
}
function start(e){
alert('function start was called');
db = e.target.result;
chkAdmin();
showUsers();// Show all values in the object store
}
function createdb(e){
var datababase = e.target.result;
var myusers = datababase.createObjectStore('users', {keyPath: 'userName'});
}
function addObject(){
if(confirm('Are you sure you want to resgister?')){
var fName = document.getElementById('fName').value;
var lName = document.getElementById('lName').value;
var userName = document.getElementById('uName').value;
var pass = document.getElementById('password').value;
var email = document.getElementById('email').value;
var dob = document.getElementById('dob').value;
var phone = document.getElementById('phone').value;
var bio = document.getElementById('bio').value;
var terms = document.getElementById('terms').value;
var school = document.getElementById('school').value;
//May need to set a loop to find value of radio
var gender;
var radios = document.getElementsByName('gender');
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
gender=radios[i].value;
}
}
//set up transaction
var mytransaction = db.transaction(['users'], "readwrite");
//get object store
var myusers = mytransaction.objectStore('users');
//Add item
var request = myusers.add(new getUser(userName,fName,lName,pass,email,dob,phone,bio,terms,school,gender));
}
// Show all results.
mytransaction.addEventListener('complete', showUsers);
//Reset Form Fields
resetForm();
}
function getUser(userName, fn, ln, pw, em, dob, tel, bio,tm, scl, gender){
this.userName = userName;
this.fn = fn;
this.ln = ln;
this.pw = pw;
this.em = em;
this.dob = dob;
this.tel = tel;
this.bio = bio;
this.tm = tm;
this.scl = scl;
this.gender = gender;
}
答案 0 :(得分:1)
如果在对象库中找不到对象,则不会返回错误。只有在传递无效密钥等问题时才会调用onerror。
event.target.result - &gt;如果没有找到,这将是未定义的。您需要执行以下操作:
db.transaction("users").objectStore("users").get(user).onsuccess = function(event) {
var x = event.target.result;
if(!x) {
alert('Sorry this is not a valid username');
}
else if(pass !== x.pw ){
alert('Sorry, wrong password');
}
顺便提一下。我永远不会告诉用户他使用的用户名无效或密码无效。最好说“组合用户名/密码错误。”否则你会让那些意图不好的人更容易;)。
其他方节点。我认为在indexeddb中存储密码并不安全。