this.request = indexedDB.open('myDatabase', 1) this.request.onerror = function(event:any) { console.log('打开数据库出错!') } this.request.onupgradeneeded = function(event:any) { console.log('创建执行') const db = event.target.result // 创建一个名为“myStore”的对象存储 const store = db.createObjectStore('myStore', { keyPath: 'id' }) // 添加一个名为“name”的索引 store.createIndex('name', 'name', { unique: false }) } this.request.onsuccess = function(event:any) { const transaction = db.transaction(['myStore'], 'readonly') const objectStore = transaction.objectStore('myStore') const index = objectStore.index('name') const cursor = index.openCursor() cursor.onsuccess = (event:any)=> { } transaction.oncomplete = function(event:any) { db.close() } }上文中cursor.onsuccess中的函数为什么执行两次
时间: 2024-02-14 11:23:17 浏览: 21
可能是因为openCursor()方法会返回一个IDBCursor对象,该对象可以用于遍历数据存储库中的数据,并通过多次调用onsuccess事件来检索每个匹配项。因此,当有多个匹配项时,可能会触发多次onsuccess事件。如果你想要遍历所有匹配项,可以在onsuccess事件处理程序中使用continue方法来移动游标并获取下一个匹配项。
相关问题
ts中class FMation3DApp { constructor(element:HTMLElement, options:Object) { this.init(element,options) } init(element:HTMLElement, options:Object) { this.request = indexedDB.open('myDatabase', 1) } }优化
根据你提供的代码,我看到你在 `init` 函数中声明了一个公共属性 `request`,并将其赋值为 `indexedDB.open('myDatabase', 1)`,但是没有进行类型声明。为了优化这段代码,你可以使用 TypeScript 的类型检查功能,显式地声明 `request` 的类型,例如:
```typescript
class FMation3DApp {
request: IDBOpenDBRequest; // 声明 request 的类型为 IDBOpenDBRequest
constructor(element: HTMLElement, options: object) {
this.init(element, options);
}
init(element: HTMLElement, options: object) {
this.request = indexedDB.open('myDatabase', 1);
}
}
```
在上面的代码中,我们使用 `IDBOpenDBRequest` 显式地声明了 `request` 的类型,这样 TypeScript 编译器就会在编译时检查 `request` 的类型,避免类型错误。
class FMation3DApp { request: IDBOpenDBRequest | undefined // 浏览器数据库 renderer: any // 创建渲染器对象 camera:any // 摄像机对象 scene:any // 三维场景 constructor(element:HTMLElement, options:Object) { this.init(element,options) } init(element:HTMLElement, options:Object) { this.request = indexedDB.open('myDatabase', 1) this.request.onsuccess = function(event:any) { const cursor = index.openCursor() cursor.onsuccess = function(event:any) { this.scene = new THREE.Scene() // 创建scene对象 } } } setSprite(){ // 创建精灵模型 const spriteMaterial = new THREE.SpriteMaterial({ map: new THREE.TextureLoader().load(aaa) }) const sprite = new THREE.Sprite(spriteMaterial) // 设置精灵模型尺寸 sprite.scale.set(10, 10, 1) console.log('精灵模型',sprite,this.scene,this.renderer) // 添加精灵模型到场景中 this.scene.add(sprite) } }中this.scene为空
在你提供的修改后的代码中,仍然存在一个小错误。在`cursor.onsuccess`回调函数中,`this`关键字指向的是`cursor`对象,而不是`FMation3DApp`对象。如果你想要在`cursor.onsuccess`回调函数中访问`FMation3DApp`对象的属性,你需要将`this`存储到另一个变量中,以便在回调函数中使用。例如:
```
init(element:HTMLElement, options:Object) {
this.request = indexedDB.open('myDatabase', 1)
const self = this // 存储this
this.request.onsuccess = function(event:any) {
const cursor = index.openCursor()
cursor.onsuccess = function(event:any) {
self.scene = new THREE.Scene() // 创建scene对象,使用self而不是this
}
}
}
```
这样,当`cursor.onsuccess`回调函数被调用时,你可以使用`self`变量来访问`FMation3DApp`对象的属性,而不是使用`this`关键字。这应该可以解决`this.scene`为空的问题。