【问题标题】:Dexie.js table.name isn't working even though the table is under the tables property即使表格位于表格属性下,Dexie.js table.name 也不起作用
【发布时间】:2018-12-17 15:38:39
【问题描述】:

我想将表中的所有项目提取到集合中,但收到表名称为 undefined 的错误消息。这是我的商店:

db.version(1).stores({
  users: '++id,',
  orgs: '++id,',
  applications: '++id'
})

然后这是我的电话:

db.orgs.toCollection().count(function (count) {
   console.log(count)
})

它给出了以下错误:

TypeError: Cannot read property 'toCollection' of undefined

但是当我在调用时停止调试器并输入db.tables 果然:

1:Table {name: "orgs", schema: TableSchema, _tx: undefined, …}
_tx:undefined
hook:function rv(eventName, subscriber) { … }
name:"orgs"

感谢任何帮助 - 谢谢。

更新

我注意到,当我在初始创建时为数据库播种时,我可以提取数据。所以我将该代码复制到我的模板中。但是它仍然失败,所以我必须缺少一些简单的东西,这是代码:

import Dexie from '@/dexie.es.js'

export default {
  name: 'ListOrgs',
  data: () => {
    return {
      orgs: []
    }
  },
  methods: {
    populateOrgs: async function () {
      let db = await new Dexie('myDatabase').open()
      db.orgs.toCollection().count(function (count) {
        console.log(count)
      })
    }
  },
  mounted () {
    this.populateOrgs()
  }
}

【问题讨论】:

  • 为了补充这一点,我尝试创建一个工厂来创建样板文件并打开数据库。在最初创建数据库并播种数据时在此文件中,我可以调用它并且它可以工作。但是,即使直接使用 Dexie 也会抛出同样的错误。

标签: vue.js dexie


【解决方案1】:

Dexie 有两种模式

  • 静态 - 大多数示例中最常用的一种。
  • 动态 - 代码中未指定架构。

静态模式

//
// Static Mode
//
const db = new Dexie('myDatabase');
db.version(1).stores({myTable1: '++'});
db.version(2).stores({myTable1: '++, foo'});
db.myTable1.add({foo: 'bar'}); // OK - dexie knows about myTable1!

动态模式

//
// Dynamic Mode
//
const db = new Dexie('myDatabase');
// FAIL: db.myTable1.add({foo: 'bar'}); // myTable1 is unknown to the API.
// Here, you must wait for db to open, and then access tables using db.table() method:
db.open().then(db => {
  const myTable = db.table('myTable');
  if (myTable) {
    myTable.add({foo: 'bar'});
  }
}).catch(error => {
  console.error(error);
});

如果省略任何 version() 规范,Dexie 将尝试打开任何具有相同名称的现有数据库,无论版本或架构如何。但它不会在 db 实例上创建隐式表属性。

当动态模式有用时

在构建应适应任何 indexedDB 数据库(例如数据库浏览器)的任意数据库实用程序时,动态模式非常有用。当 javascript 代码在设计上不知道架构时(期望查询哪些表以及存在哪些索引),动态模式也很有用。

静态模式的好处

  • 无需等待 db.open() 完成。
  • 需要时自动创建数据库。无需复杂的应用代码来处理数据库版本控制。
  • 需要时自动DB population

静态模式下的设计模式

db.js

import Dexie from 'dexie';

//
// Let this module do several things:
//
//  * Create the singleton Dexie instance for your application.
//  * Declare it's schema (and version history / migrations)
//  * (Populate default data http://dexie.org/docs/Dexie/Dexie.on.populate)
// 

export const db = new Dexie('myDatabase');

db.version(1).stores({
  users: '++id,',
  orgs: '++id,',
  applications: '++id'
});

db.on('populate', () => {
  return db.orgs.bulkAdd([
    {'foo': 'bar'},
  ]);
});

app.js

import {db} from './db';

// Wherever you use the database, include your own db module
// instead of creating a new Dexie(). This way your code will
// always make sure to create or upgrade your database whichever
// of your modules that comes first in accessing the database.
//
// You will not have to take care of creation or upgrading scenarios.
//
// Let Dexie do that for you instead.
// 

async function countOrgs() {
  return await db.orgs.count();
}

【讨论】:

    猜你喜欢
    • 2020-05-25
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多