mWater / minimongo

Client-side in-memory mongodb backed by localstorage with server sync over http
GNU Lesser General Public License v3.0
1.18k stars 131 forks source link

[Bug] Occur 'transaction' of null at addCollection or removeCollection on IndexedDb backend #13

Closed mizchi closed 9 years ago

mizchi commented 9 years ago
Possibly unhandled TypeError: Cannot read property 'transaction' of null
  at [object Object].IDBStore.iterate (/Users/mizchi/proj/kobito-shell/shell/node_modules/minimongo/node_modules/idb-wrapper/idbstore.js:1033:38)
  at [object Object].IDBStore.query (/Users/mizchi/proj/kobito-shell/shell/node_modules/minimongo/node_modules/idb-wrapper/idbstore.js:1093:19)
  at IndexedDb.module.exports.IndexedDb.removeCollection (/Users/mizchi/proj/kobito-shell/shell/node_modules/minimongo/lib/IndexedDb.js:60:23)

This log occurs at atom-shell envrironment but also occur at Chrome.

I tried 2 patterns but both failed. (sorry for coffeescript)

case 1

db = new minimongo.IndexedDb('testdb')
db.addCollection('a')
db.removeCollection('a')
db.addCollection('a')
db.a.find({})

case 2

db = new minimongo.IndexedDb('testdb')
db.addCollection 'a', ->
  db.removeCollection 'a', ->
    db.addCollection 'a', ->
      db.a.find {}

I added setTimeout before find so it works

db = new minimongo.IndexedDb('testdb')
db.addCollection('a')
db.removeCollection('a')
db.addCollection('a')
setTimeout ->
  db.a.find({})
, 100

I doubt minimongo lost correct callback of IndexedDb ready.

grassick commented 9 years ago

You need to wait for the callback from the constructor. See the example in the Readme:

// Require minimongo
var minimongo = require("minimongo");

var IndexedDb = minimongo.IndexedDb;

// Create IndexedDb
db = new IndexedDb({namespace: "mydb"}, function() {
    // Add a collection to the database
    db.addCollection("animals", function() {
        doc = { species: "dog", name: "Bingo" };

        // Always use upsert for both inserts and modifies
        db.animals.upsert(doc, function() {
            // Success:

            // Query dog (with no query options beyond a selector)
            db.animals.findOne({ species:"dog" }, {}, function(res) {
                console.log("Dog's name is: " + res.name);
            });
        });
    });
}, function() { alert("some error!"); });

Or, better:

new IndexedDb({namespace: "mydb"}, function(db) {
  // Do something with db, such as add a collection.
});