2013-03-23 67 views
0

我有一個帶有索引數據庫的註冊表。我有resgister /登錄工作。我一直在嘗試不同的事情來獲得一個新的註冊登記,只要他們註冊,但我不知道發生了什麼。它不會工作。 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

回答

0

它看起來像你使用的loginCheck方法onsuccess處理程序,但你不能調用數據庫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(); 
} 
+0

我試過,但它給我的錯誤,用戶是不是在數據庫,並當我檢查我的數據庫在康壽用戶密鑰是存在的,但對象爲null。我試圖改變oncomplete,但也沒有幫助 – Batman 2013-03-23 19:50:56

相關問題