我想将表中的所有项目提取到集合中,但出现表名称为undefined. 这是我的商店:
db.version(1).stores({
users: '++id,',
orgs: '++id,',
applications: '++id'
})
Run Code Online (Sandbox Code Playgroud)
后来这是我的电话:
db.orgs.toCollection().count(function (count) {
console.log(count)
})
Run Code Online (Sandbox Code Playgroud)
它给出了以下错误:
TypeError: Cannot read property 'toCollection' of undefined
Run Code Online (Sandbox Code Playgroud)
但是当我在调用时停止调试器并输入db.tables足够的内容时:
1:Table {name: "orgs", schema: TableSchema, _tx: undefined, …}
_tx:undefined
hook:function rv(eventName, subscriber) { … }
name:"orgs"
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏 - 谢谢。
更新
我注意到当我在初始创建时为数据库播种时,我可以取出数据。所以我将该代码复制到我的模板中。然而,它仍然失败了,所以一定有一些简单的东西我错过了,这是代码:
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()
}
}
Run Code Online (Sandbox Code Playgroud)
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!
Run Code Online (Sandbox Code Playgroud)
//
// 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);
});
Run Code Online (Sandbox Code Playgroud)
如果省略任何 version() 规范,Dexie 只会尝试打开任何具有相同名称的现有数据库,无论版本或架构如何。但它不会在 db 实例上创建隐式表属性。
动态模式在构建适用于任何 indexedDB 数据库的任意数据库实用程序时非常有用 - 例如 DB 资源管理器。当 javascript 代码设计为不知道架构(预期查询哪些表以及有哪些索引)时,动态模式也很有用。
数据库.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'},
]);
});
Run Code Online (Sandbox Code Playgroud)
应用程序.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();
}
Run Code Online (Sandbox Code Playgroud)