0

我有一个带有 indexedDB 的注册表单。我已经注册/登录工作。我一直在尝试不同的方法来让新的注册器在注册后立即登录,但我不确定发生了什么。它行不通。localStorage 像它应该设置的那样设置,但它不会显示用户的数据。

JS:

function addObject(){
    if(confirm('Are you sure you want to resgister?')){
     fName = document.getElementById('fName').value;
     lName = document.getElementById('lName').value;
     userName = document.getElementById('uName').value;
     pass = document.getElementById('password').value;
     email = document.getElementById('email').value;
     dob = document.getElementById('dob').value;
     tel = document.getElementById('tel').value;
     bio = document.getElementById('bio').value;
     terms = document.getElementById('terms').value;
     school = document.getElementById('school').value;

     //Need loop to find radio button values
     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.put(new getUser(userName,fName,lName,pass,email,dob,tel,bio,terms,school,gender));
    }

    // Show all results.
    if(document.URL.indexOf('admin.html') > -1){
        mytransaction.addEventListener('complete', showUsers);  
    } 

    //Log in new users
    loginCheck(userName,pass);
    /*newLocal('user',user); */
    //Reset Form Fields
    resetForm(); 

}

这是我在有人登录时使用的功能。

function loginCheck(user,pass){    
db.transaction("users").objectStore("users").get(user).onsuccess = function(e) {
    var loggedUser = e.target.result;
        if(!loggedUser){
            alert('Sorry, Username does not exist. Please try again.');
        }else if(pass !== loggedUser.pw ){
            alert('Incorrect log in combination. Please try again.');
            }else{
                loggedIn(loggedUser);
                populateFields(loggedUser);
                loginStyle(loggedUser)
                }        
    }    
}
function loggedIn(loggedUser){
    var u=loggedUser;
    alert('Welcome '+u.fn+' '+u.ln+' to Macroplay');       

    //Set localStorage     
    var signedin = 'user';
    var username = u.userName;
    newLocal(signedin,username);
}
function getValues(){ 
db.transaction("users").objectStore("users").get(localStorage.user).onsuccess = function(e) { 
    populateFields(e.target.result); 
    loginStyle(e.target.result)
    }; 
    ;
}

从我所看到的第一部分将用户添加到数据库中,第二部分的行为应该与我刚刚正常登录时一样。但它不会从数据库中提取信息。

我尝试将新用户设置为 localStorage,这是我识别当前登录用户的方式,但这也没有效果。

这是我的网站:http ://www3.carleton.ca/clubs/sissa/html5

4

1 回答 1

0

看起来您在方法中使用了onsuccess处理程序loginCheck,但在调用put数据库时却没有。我的猜测是,您正在检查用户是否存在,然后才真正有机会被添加。尝试这个:

//Add item
var request = myusers.put(new getUser(userName,fName,lName,pass,email,dob,tel,bio,terms,school,gender));
request.onsuccess = function () {

    // Show all results.
    if(document.URL.indexOf('admin.html') > -1){
        mytransaction.addEventListener('complete', showUsers);  
    } 

    //Log in new users
    loginCheck(userName,pass);
    /*newLocal('user',user); */
    //Reset Form Fields
    resetForm(); 
}
于 2013-03-23T16:16:12.633 回答