使用angular2向indexedDB添加值时出错

使用angular2向indexedDB添加值时出错

本文介绍了使用angular2向indexedDB添加值时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular2使用IndexedDB.为此,我正在下面的github链接. https://github.com/gilf/angular2-indexeddb

I am using IndexedDB using Angular2. For that i am following the below github link. https://github.com/gilf/angular2-indexeddb

我正在像这样创建数据库,并像这样增加价值.

I am creating db like this and adding value like this.

    ngOnInit(){

        let db = new AngularIndexedDB('mydb', 1);
        db.createStore(1, (evt) => {
            let objectStore = evt.currentTarget.result.createObjectStore(
                'Userdetails', { keyPath: "id", autoIncrement: true });

            objectStore.createIndex("name", "name", { unique: false });
            objectStore.createIndex("email", "email", { unique: true });

        });


db.add('Userdetails', { name: 'name', email: '[email protected]' }).then(() => {
    // Do something after the value was added
     console.log('fields added');
}, (error) => {
    console.log('error is'+error);
});

}

但是在控制台中,我遇到这样的错误.在查询数据库之前,您需要使用createStore函数创建数据库!

But in console i am getting error like this.You need to use the createStore function to create a database before you query it!

任何人都可以告诉我我到底在哪里做错了.我对这个angular2-indexeddb非常陌生.请帮助我.

Can anyone please tell me where exactly i am doing wrong.I am very new to this angular2-indexeddb.Please help me.

推荐答案

如果我正确阅读代码,我认为您的db.add代码会被开除.

I think your code for db.add is fired to early if i'm reading the code correctly.

如果将其更改为它应该可以使用

If you change it to it should work

 db.createStore(1, (evt) => {
        let objectStore = evt.currentTarget.result.createObjectStore(
            'Userdetails', { keyPath: "id", autoIncrement: true });

        objectStore.createIndex("name", "name", { unique: false });
        objectStore.createIndex("email", "email", { unique: true });

}).then(() => {
    db.add('Userdetails', { name: 'name', email: '[email protected]' }).then(() => {
        // Do something after the value was added
        console.log('fields added');
    }, (error) => {
        console.log('error is'+error);
    });
});

这篇关于使用angular2向indexedDB添加值时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 00:15