About how IDBDatabase objects in indexedDB have been generated but cannot call the createObjectStore method to create namespaces?

the IDBDatabase object in indexedDB has been generated but cannot call the createObjectStore method to create a namespace, prompting Uncaught TypeError: Cannot read property "createObjectStore" of undefined.
here is the code

    var request = indexedDB.open("jusing"),
        db;
        user = {
            username: "jusing",
            age: 18,
            gender: "male",
            handsome: true
        }
    request.onsuccess = function() {
        db = event.target.result;
    }
    var store = db.createObjectStore("users", {keyPath: "username"});
    store.add(user);
May.29,2021

first of all, db is not assigned at all, and then createObjectStore cannot be used in onsuccess .

to update the schema, of the database, that is, to create or delete the object storage space, you need to implement the onupgradeneeded handler, which will be called as part of a versionchange transaction that allows you to handle the object storage space.

Using_IndexedDB

var request = indexedDB.open("jusing"),
    db;
user = {
    username: "jusing",
    age: 18,
    gender: "male",
    handsome: true
}
request.onupgradeneeded  = function() {
    db = event.target.result;
    var store = db.createObjectStore("users", { keyPath: "username" });
    store.add(user);
}
Menu