Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript IDB:openCursor()不是函数_Javascript_Cursor_Indexeddb - Fatal编程技术网

Javascript IDB:openCursor()不是函数

Javascript IDB:openCursor()不是函数,javascript,cursor,indexeddb,Javascript,Cursor,Indexeddb,也许我做错了什么。。。但这个错误非常令人困惑。我坚持使用:uncaughttypeerror:requestChatHistory.openCursor不是IDBDOpenDBRequest.dbPromise.onsuccess中的函数 IDBROpenRequest可能从IDBRequest继承方法 我尝试在没有openCursor的情况下发出请求,并从中获取第一个值。 还尝试重新创建数据库,使用不同的浏览器Chrome、Edge、Firefox onUpgrade承诺: dbPromise

也许我做错了什么。。。但这个错误非常令人困惑。我坚持使用:uncaughttypeerror:requestChatHistory.openCursor不是IDBDOpenDBRequest.dbPromise.onsuccess中的函数

IDBROpenRequest可能从IDBRequest继承方法

我尝试在没有openCursor的情况下发出请求,并从中获取第一个值。 还尝试重新创建数据库,使用不同的浏览器Chrome、Edge、Firefox

onUpgrade承诺:

dbPromise.onupgradeneeded = function(event) { 
        let db = event.target.result;
        db.createObjectStore('keys', {keyPath: 'userId'});
        let chatHistory = db.createObjectStore('messages', { keyPath: "id", autoIncrement:true });
        chatHistory.createIndex("chatId", "chatId", { unique: false });
    };
这是我的功能和请求:

async function loadSavedMessages(chatId){
        let dbPromise = idb.open('clientDB', 3);
        dbPromise.onsuccess = function() {
            let db = this.result;
            let dbTransaction = db.transaction(["messages"]);
            let messages = dbTransaction.objectStore("messages");
            let index = messages.index('chatId'); 
            let requestChatHistory = index.get(chatId);
            requestChatHistory.openCursor().onsuccess = function(event) {
                let cursor = event.target.result;
                if (cursor) {
                    console.log(cursor);
                    cursor.continue();
                }
            };
        }
    }
保存数据的功能:

async function saveMessage(chatId, message, userId){
        let dbPromise = idb.open('clientDB', 3);
        dbPromise.onsuccess = function() {
            let db = this.result;
            let dbTransaction = db.transaction(["messages"], 'readwrite');
            let messages = dbTransaction.objectStore("messages");
            let mesObj = {
                chatId: chatId,
                user: userId,
                message: message,
                timestamp: Date.now()
            };
            let save = messages.add(mesObj);
            save.onerror = function(event) {
                // Handle errors!
                console.log("Something went wrong with local DB :(")
            };
            save.onsuccess = function(event) {
                // Do something with the request.result!
                console.log(`Message saved, id ${save.result}`);
            };
        }
    }
这就是你的问题:

            let requestChatHistory = index.get(chatId);
            requestChatHistory.openCursor().onsuccess = function(event) {
requestChatHistory是一个,没有openCursor方法。openCursor处于启用状态,就像您的索引变量一样。所以也许你想做一些类似的事情:

            index.openCursor(chatId).onsuccess = function(event) {