gpt4 book ai didi

javascript - IDB : openCursor() is not a function

转载 作者:行者123 更新时间:2023-12-02 23:40:29 25 4
gpt4 key购买 nike

好吧,也许我做错了什么......但是这个错误非常令人困惑。我坚持:“未捕获的类型错误:requestChatHistory.openCursor 不是 IDBOpenDBRequest.dbPromise.onsuccess 的函数”

IDBOpenRequest 可能会继承 IDBRequest 的方法...

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

onUpgrade promise :

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) {

requestChatHistoryIDBRequest ,它没有 openCursor 方法。 openCursor 位于 IDBIndex ,就像您的 index 变量一样。所以也许你打算做类似的事情:

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

关于javascript - IDB : openCursor() is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56101928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com