IndexedBD和localStorage未定义错误

时间:2013-03-20 01:33:37

标签: javascript html5 local-storage undefined indexeddb

我有一个indexedDB,它试图用来捕获用户注册的表单信息。该部分工作正常,但教授希望创建一个帐户,并在创建时设置用户名和密码,以便他可以登录。

我接触到的方式是使用localStorage API。我创建了一个函数来检查是否曾创建过admin帐户,如果没有创建它,则调用addAdmin()函数。

我尝试通过复制addObject()创建addAdmin()但由于某种原因,我的db变量在控制台中以undefined形式返回。

错误“未捕获的TypeError:无法调用未定义的方法'事务'

var mainForm, fName, lName, uName, pass, email, dob, phone, bio, nl, terms, school, gender, save, reset, db;




//-------------USER DB------------------//
function startDB(){
    mainForm = document.getElementById('mainFormSidebar');
    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');
    nl = document.getElementById('newsletter');
    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');

    mainForm.addEventListener('submit',addObject);

    //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
    chkAdmin();
}
function createdb(e){
    var datababase = e.target.result;
    var myusers = datababase.createObjectStore('users', {keyPath: 'userName'});
}
function start(e){
    db = e.target.result;


showUsers();// Show all values in the object store
}

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 nl = document.getElementById('nl').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,nl,terms,school,gender));
    }

    // Show all results.
    mytransaction.addEventListener('complete', showUsers);  

    //Reset Form Fields
    resetForm();


}
function getUser(userName, fn, ln, pw, em, dob, tel, bio, nl,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.nl = nl;
    this.tm = tm;
    this.scl = scl;
    this.gender = gender;
}
//------Create Admin Account-----//
function chkAdmin(){
    alert('before adding admin'); 
     if(localStorage.getItem('admin')!="added"){             
        alert('adding admin');
        addAdmin();
        alert('admin added');
    }
}
function addAdmin(){
    //set up transaction
        var mytransaction = db.transaction(['users'], "readwrite"); 
    //get object store
        var myusers = mytransaction.objectStore('users'); 
        var request = myusers.add(new getUser('admin','Shawn','Smith-Choa','admin'));  
        request.addEventListener('success',showUsers);   
    //Locally store that admin as been created  
        var admin = 'admin'; 
        var value = 'added';
        newItem(admin,value); 
}

//-------------Web Storage API------------//
function newItem(id,style){
    localStorage.setItem(id,style);       
}

1 个答案:

答案 0 :(得分:0)

您没有将db的值分配给任何内容,因此始终未定义。我认为你的意思是在createdb方法中进行,但实际上你应该在start方法中捕获/分配它success处理程序将触发(我也不能在任何地方找到start方法