首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Dexie未创建索引数据库

使用Dexie未创建索引数据库
EN

Stack Overflow用户
提问于 2022-01-23 01:27:25
回答 1查看 429关注 0票数 0

我有一个角度应用程序,我试图插入到一个索引数据库。问题是数据库没有被创建。

下面是代码中的一节,我在其中插入了一个数据库条目。

代码语言:javascript
运行
复制
  createImageFromBlob(ficname:any , image: Blob) {
    let reader = new FileReader();
    reader.addEventListener("load", (evt) => {
      let res = evt.target.result;

      // Store Data URL in localStorage
      try {
        if (typeof res === "string") {
          localStorage.setItem(ficname, res);
          this.dixieDbService.addNewImage(ficname, res).then(r => console.log("inserted: "));
        }
      }
      catch (e) {
        console.log("Storage failed: " + e);
      }
    }, false);
    if (image) {
      reader.readAsDataURL(image);
    }
  }

下面是bd.ts

代码语言:javascript
运行
复制
import Table = Dexie.Table;
import Dexie from "dexie";

export interface ImageList {
  id?: number;
  imgName: string;
  imgDataUrlValue: string|ArrayBuffer;
}


export class AppDB extends Dexie {
  imageList!: Table<ImageList, number>;


  constructor() {
    super('ngdexieliveQuery');
    this.version(3).stores({
      imageList: '++id'
    });
   // this.on('populate', () => this.populate());
  }
  
}

export const db = new AppDB();

以下是服务类别:

代码语言:javascript
运行
复制
import {Injectable} from "@angular/core";
import {liveQuery} from "dexie";
import {db, ImageList} from "../dixie/db";

@Injectable()
export class DixieIndexedDbService {

  imageList$ = liveQuery(() => db.imageList.toArray());
  listName = 'My local image list';

  async addNewImage(imgName:string, imgDataVal:any) {
    await db.imageList.add({
      imgName: imgName,
      imgDataUrlValue:imgDataVal
    });
  }

  async getImgDataValue(imgName:string) {
    return db.imageList.get({imgName: imgName});
  }


}

我有stackblitz上的示例工作代码:

https://stackblitz.com/edit/angular-ivy-znzhv8

EN

回答 1

Stack Overflow用户

发布于 2022-01-23 06:06:07

在调用Dexie的构造函数时没有提供任何选项,以便创建数据库.

open

  • Or,调用
  1. 向数据库发出查询。为了在上面(OP)的情况下,在函数createImageFromBlob中调用load事件处理程序,应该调用DexieService方法,该方法应该作为指定实现的一部分在数据库上发出查询。发布这篇文章,我们应该有一个数据库。

AppDB类调用open..。

代码语言:javascript
运行
复制
import Table = Dexie.Table;
import Dexie from "dexie";

export interface ImageList {
  id?: number;
  imgName: string;
  imgDataUrlValue: string|ArrayBuffer;
}


export class AppDB extends Dexie {
  imageList!: Table<ImageList, number>;


  constructor() {
    super('ngdexieliveQuery');
    this.version(3).stores({
      imageList: '++id'
    });
    this.open(); // <---- Missing in OP
   // this.on('populate', () => this.populate());
  }
  
}

export const db = new AppDB();
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70818486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档