angular - 使用 Angular 7 在 IndexedDB 中添加价值
问题描述
首先,我尝试了与该主题相关的所有问题和答案。此外,我尝试了相关问题并尝试解决但没有成功。所以请仔细阅读我的问题。
首选链接
与链接相关的搜索
我想在其中添加数据,indexeddb
但以某种方式显示数据库连接打开错误。我附上了一个截图。所以我要求回答一个小例子增加价值indexeddb
。
我的代码
app.components.js
import { Component, OnInit } from '@angular/core';
import { NgxIndexedDB } from 'ngx-indexed-db';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'indexDb-Angular';
ngOnInit() {
let db = new NgxIndexedDB('DVdb', 1);
db.openDatabase(1, evt => {
let objectStore = evt.currentTarget.result.createObjectStore('people', { keyPath: 'id', autoIncrement: true });
objectStore.createIndex('name', 'name', { unique: false });
objectStore.createIndex('email', 'email', { unique: true });
});
db.add('people', { name: 'Sumit', email: 'Sumit@test.com' }).then(
() => {
// Do something after the value was added
},
error => {
console.log(error);
}
);
}
}
解决方案
创建一个异步方法,以便在openDatabase()
添加值之前等待数据库打开。您可以通过使用使其异步.then(function() {
ngOnInit() {
let db = new NgxIndexedDB('DVdb', 1);
db.openDatabase(1, evt => {
let objectStore = evt.currentTarget.result.createObjectStore('people', { keyPath: 'id', autoIncrement: true });
objectStore.createIndex('name', 'name', { unique: false });
objectStore.createIndex('email', 'email', { unique: true });
}).then(function () {
db.add('people', { name: 'Sumit', email: 'Sumit@test.com' }).then(
() => {
// Do something after the value was added
},
error => {
console.log(error);
}
);
});
}
推荐阅读
- android - Web3J - 创建轻钱包的时间太长
- php - 我可以在 WAMP 服务器上运行 Angular 6 项目吗?
- ethereum - 通过另一个第三方合约发送 ERC721 代币
- express - Apollo-Server,如何与 SailsJS 一起使用?
- javascript - 使 vimeo 或 youtube 在模态打开时播放并在模态关闭时停止视频播放
- hyperledger-fabric - LDAP 上的 Hyperledger Fabric-ca 连接
- android - Android studio - 当很多口味(> 70)时同步 gradle 需要很长时间
- sql - SQL Anywhere - SELECT COUNT(PK) WHERE [条件] 性能
- android - MPAndroidChart:如何为 PieChart 中的标签设置不同的颜色?
- python - 基于时间将多个数据框加入file.txt python