Here is my code:
function set() {
var db;
var request;
var store;
var username = document.getElementById("username");
var password = document.getElementById("password");
var newuser = {
"username": username.value,
"password": password.value,
};
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
if ('webkitIndexedDB' in window) {
window.IDBTransaction = window.webkitIDBTransaction;
window.IDBKeyRange = window.webkitIDBKeyRange;
}
request = indexedDB.open("userdb", 1);
request.onerror = function(event) {};
request.onsuccess = function(event) {
db = event.target.result;
var transaction = db.transaction(["user"], "readwrite");
var objectStore = transaction.objectStore("user");
var addrequest = objectStore.add(newuser);
addrequest.onsuccess = function(event) {
alert("Success");
};
};
request.onupgradeneeded = function(event) {
db = event.target.result;
store = db.createObjectStore("user", {
keyPath: "username",
autoIncrement: false
});
};
}
After insert data,I use function get() to get data from indexedDB,but I failed to get that,What is the problem?Thank you!
function get() {
var db;
var request;
var store;
var username = document.getElementById("username");
var password = document.getElementById("password");
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
if ('webkitIndexedDB' in window) {
window.IDBTransaction = window.webkitIDBTransaction;
window.IDBKeyRange = window.webkitIDBKeyRange;
}
request = indexedDB.open("userdb", 1);
request.onerror = function(event) {
alert("Error Code:" + event.target.errorCode);
};
request.onsuccess = function(event) {
db = event.target.result;
var transaction = db.transaction("user", IDBTransaction.READ_WRITE);
var objectStore = transaction.objectStore("user");
objectStore.get(username.value);
if (password.value == event.target.result.password)
alert("Success“);
else
alert("Failed");
};
request.onupgradeneeded = function(event) {
alert("Update Success");
};
}
The function works after click another button to get data.But I failed to get data.I have added newuser(username:1 password:1) to the table user.But I cann't get the data.Why?Thank you for your answer.