javascript - 我可以将 javascript 类对象存储在索引数据库中吗?
问题描述
我用构造函数和一些方法创建了一个名为 Book 的类。
class Book {
constructor(title, author) {
this.title = title;
this.author = author;
}
displayInfo() {
console.log(`Title: ${this.title}, Author: ${this.author}`);
}
}
然后我创建了一堆 Book 对象,并将它们放在一个名为 Library 的数组中。
const book1 = new Book('Hobbit', 'JRR Tolkien');
const book2 = new Book('Mistborn', 'Brandon Sanderson');
let library = [];
library.push(book1);
library.push(book2);
console.log(library);
library.forEach(book => book.displayInfo());
然后我将这个库数组存储在 indexeddb 中。
if (!window.indexedDB) {
window.alert("IndexedDB doesn't work");
}
let request = indexedDB.open("Database", 1);
let db, tx, store;
request.onupgradeneeded = function(e) {
let db = request.result;
let store = db.createObjectStore('Collection', { keyPath: 'type' });
};
request.onerror = function(e) {
console.log('error ' + e.target.errorCode);
};
request.onsuccess = function(e) {
db = request.result;
tx = db.transaction('Collection', 'readwrite');
store = tx.objectStore('Collection');
db.onerror = function(e) {
console.log('error ' + e.target.errorCode);
};
console.log('adding do collection db');
store.put({ type: 'library', data: library });
tx.oncomplete = function() {
db.close();
};
};
现在我想在每次页面刷新后恢复这个数组,并对存储在数组中的对象运行 Book 方法。但是当我恢复数据时,我发现我的 Book 对象不再是 Book 对象,只是常规对象,所以我不能对它们使用 Book 方法。
if (!window.indexedDB) {
window.alert("IndexedDB doesn't work");
}
let request = indexedDB.open("Database", 1);
let db, tx, store;
request.onupgradeneeded = function(e) {
console.log('upgrading');
let db = request.result;
let store = db.createObjectStore('Collection', { keyPath: 'type' });
};
request.onerror = function(e) { console.log('error ' + e.target.errorCode); };
request.onsuccess = function(e) {
console.log('success');
db = request.result;
tx = db.transaction('Collection', 'readwrite');
store = tx.objectStore('Collection');
db.onerror = function(e) { console.log('error ' + e.target.errorCode); };
let retrievedData = store.get('library');
retrievedData.onsuccess = function(e) {
if (retrievedData.result) console.log(retrievedData);
if (retrievedData.result) library = retrievedData.result.data;
console.log(library);
library.forEach(book => book.displayInfo());
}
tx.oncomplete = function() {
db.close();
};
};
这个问题有解决方案吗?
最好的问候,奥斯卡
解决方案
这不是一个好习惯,但在检索库对象后,您可以尝试:
retrievedData.onsuccess = function(e) {
if (retrievedData.result) console.log(retrievedData);
if (retrievedData.result) library = retrievedData.result.data;
const fixedLibrary = library.map(book => Object.setPrototypeOf(book, Book.prototype))
fixedLibrary.forEach(book => book.displayInfo());
}
虽然,如果您打算将实例存储在数据库中,我建议您不要使用类,而是使用面向模块的方法......
推荐阅读
- python - 接受一个序列以及您需要找到的值,并返回一个恰好三个电阻的序列,总和为所需的值 R
- openssl - 获取 OpenSSL RSA 公钥的指纹
- regex - perl - 与正则表达式之间的范围
- docker - Symfony 在 kubernetes 上部署 - 环境问题
- python - 我在 pandas 中创建数据框时遇到了困难,该数据框取自特定的 url。有人可以照顾这个吗?
- javascript - firestore 数据库侦听器停止侦听/角度/RxJS
- javascript - 带有 null 关键字的内爆数组
- java - 在 AWS EC2 Ubuntu 18 64 位上安装 JDK 和 Maven 3.6.0
- perl - PRELOAD(和其他)常量/符号如何在 Test2::API::Instance 中工作?
- sql - 在实体框架核心中,当两个字段映射到另一个表的主键时,我们如何为一个表设置关系