【问题标题】:How do I insert an object with Linq2IndexedDB如何使用 Linq IndexedDB 插入对象
【发布时间】:2013-04-13 16:08:24
【问题描述】:

我正在通过单元测试(通过Mocha)尝试使用 Linq2IndexedDB(v. 1.0.21),但我什至无法进行简单的插入工作。发生的情况(在 Google Chrome 下运行时)是在 Linq2IndexedDB.js 的第 1535 行引发内部异常:

Uncaught TypeError: Cannot read property 'version' of undefined

我的单元测试代码如下所示;基本上只有一个测试,“它可以添加对象”:

"use strict";

define(["db", "linq2indexeddb", "chai", "underscore", "stacktrace"], function (db, linq2indexeddb, chai, _,
    printStacktrace) {
    var should = chai.should();

    describe("db", function () {
        var _db;

        function fail(done, reason, err) {
            if (typeof reason === "string") {
                reason = new Error(reason);
            }
            if (!reason) {
                console.log(typeof done, typeof reason);
                reason = new Error("There's been an error, but no reason was supplied!");
                var st = printStacktrace({e: reason});
                console.log(st);
            }
            if (typeof done !== "function") {
                throw new Error("Was not supplied a function for 'done'!");
            }
            done(reason);
        }

        // Bind done as the first argument to the fail function
        function bindFail(done, reason) {
            if (typeof done !== "function") {
                throw new Error("done must be a function");
            }
            return _.partial(fail, done, reason);
        }

        beforeEach(function (done) {
            _db = linq2indexeddb("test", null, true);
            _db.deleteDatabase()
            .done(function () {
                _db.initialize()
                .done(done)
                .fail(bindFail(done, "Initializing database failed"));
            })
            .fail(bindFail(done, "Deleting database failed"));
        });

        it("can add objects", function (done) {
            console.log("Starting test");
            var refObj = {"key": "value"};
            _db.linq.from("store").insert(refObj, "Key")
            .done(function () {
                console.log("Added object successfully");
                done();
            })
            .fail(bindFail(done, "Inserting object failed"));
        });
    });
});

我在这里做错了什么,还是 Linq2IndexedDB 中的错误(或两者都有)?

我已经建立了一个相应的测试项目on Github,并配有Karma 配置,因此您可以轻松运行包含的测试。 Karma 配置假定您已安装 Chrome。

【问题讨论】:

    标签: javascript unit-testing mocha.js karma-runner linq2indexeddb


    【解决方案1】:

    我发现了几个问题:

    1. 我的 Linq2IndexedDB 工作位置错误,应该是:'/base/lib/Linq2IndexedDB.js'
    2. 使用带外键的对象插入不起作用。

    我最终在 IE 10 和 Chrome 上进行了插入工作,尽管我仍在为 PhantomJS 苦苦挣扎。为了让它在 Chrome 下工作,我必须明确指定我的模式,我怀疑这是由于 Linq2IndexedDB 中的一个错误。我的工作解决方案如下:

    测试:

    "use strict";
    
    define(["db", "linq2indexeddb", "chai", "underscore", "stacktrace"], function (db, linq2indexeddb, chai, _,
        printStacktrace) {
        var should = chai.should();
    
        describe("db", function () {
            var _db;
    
            function fail(done, reason, err) {
                console.log("err:", err);
                if (typeof reason === "string") {
                    reason = new Error(reason);
                }
                if (!reason) {
                    console.log(typeof done, typeof reason);
                    reason = new Error("There's been an error, but no reason was supplied!");
                    var st = printStacktrace({e: reason});
                    console.log(st);
                }
                if (typeof done !== "function") {
                    throw new Error("Was not supplied a function for 'done'!");
                }
                done(reason);
            }
    
            // Bind done as the first argument to the fail function
            function bindFail(done, reason) {
                if (typeof done !== "function") {
                    throw new Error("done must be a function");
                }
                return _.partial(fail, done, reason);
            }
    
            beforeEach(function (done) {
                // Linq2IndexedDB's web worker needs this URL
                linq2indexeddb.prototype.utilities.linq2indexedDBWorkerFileLocation = '/base/lib/Linq2IndexedDB.js'
    
                _db = new db.Database("test");
    
                console.log("Deleting database");
                _db.deleteDatabase()
                .done(function () {
                    console.log("Initializing database");
                    _db.initialize()
                    .done(done)
                    .fail(bindFail(done, "Initializing database failed"));
                })
                .fail(bindFail(done, "Deleting database failed"));
            });
    
            it("can add objects", function (done) {
                console.log("Starting test");
                var refObj = {"key": "value"};
                _db.insert(refObj)
                .done(function () {
                    done();
                })
                .fail(bindFail(done, "Database insertion failed"));
            });
        });
    });
    

    实施:

    define("db", ["linq2indexeddb"], function (linq2indexeddb) {
        function getDatabaseConfiguration() {
            var dbConfig = {
                version: 1
            };
            // NOTE: definition is an array of schemas, keyed by version;
            // this allows linq2indexedDb to do auto-schema-migrations, based upon the current dbConfig.version
            dbConfig.definition = [{
                version: 1,
                objectStores: [
                { name: "store", objectStoreOptions: { keyPath: 'key' } },
                ],
                defaultData: []
            },
            ];
    
            return dbConfig;
        }
    
        var module = {
            Database: function (name) {
                var self = this;
    
                self._db = linq2indexeddb(name, getDatabaseConfiguration());
    
                self.deleteDatabase = function () {
                    return self._db.deleteDatabase();
                };
    
                self.initialize = function () {
                    return self._db.initialize();
                };
    
                self.insert = function (data) {
                    return self._db.linq.from("store").insert(data);
                };
            }
        };
        return module;
    });
    

    编辑: 它在 PhantomJS 下不起作用的原因是我在 Linq2IndexedDB 中启用了调试日志记录,出于某种原因,这似乎会在某些时候堵塞 Karma 的管道。关闭调试日志后,所有配置都有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 2013-11-24
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      相关资源
      最近更新 更多