简体   繁体   English

如何在indexedDB中存储mongodb的集合数组

[英]how to store a mongodb array of collections inside an indexedDB

Hello I want to build a local database for my phonegap so user can use it offline. 您好,我想为我的phonegap建立一个本地数据库,以便用户可以离线使用它。

I have this in angular function that creates a database. 我在创建数据库的角度函数中具有此功能。

   function Database() {

    return {
        create: function (itemDocs) {

            var db = null;

            var request = indexedDB.open("myDB", 1);

            request.onsuccess = function (event) {
                db = event.target.result;
                console.log("DB loaded successfully");

            };

            request.onerror = function (event) {
                console.log(event)
            };

            request.onupgradeneeded = function (event) {
                db = event.target.result;
                console.log("DB initiliazed / created");

                //create collections
                db.createObjectStore("items", {keyPath: "_id"});

                //create documents
                var transaction = db.transaction(["items"], "readwrite");

                var items = transaction.objectStore("items");

                items.add(itemDocs);

            };


        }
    }

}

The itemDocs holds a mongoDB collection (which is an array of objects) and I want to store that collection inside indexedDB database the problem im having is that I'm getting this annoying error. itemDocs拥有一个mongoDB集合(这是一个对象数组),我想将该集合存储在indexedDB数据库中,问题是我遇到了这个烦人的错误。

Uncaught InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': A version change transaction is running.

Use var transaction = event.target.transaction instead of var transaction = db.transaction(...); 使用var transaction = event.target.transaction而不是var transaction = db.transaction(...);

A full answer is rather lengthy. 完整的答案相当冗长。 Briefly, you don't want to create a new transaction in onupgradeneeded. 简而言之,您不需要在onupgradeneed上创建新事务。 There is already an active transaction available for you. 您已经有一个有效的交易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM