knadh / localStorageDB

A simple database layer for localStorage and sessionStorage for creating structured data in the form of databases and tables
http://nadh.in/code/localstoragedb
814 stars 128 forks source link

insertOrUpdate - Cannot read property 'toString' of null #53

Closed danbuntu closed 8 years ago

danbuntu commented 9 years ago

I'm trying to use insertOrUpdate and I'm getting the error:

Cannot read property 'toString' of null

I'm using the following code;

lib.insertOrUpdate("settings", {name: 'sound'}, { name: "sound", gameState: "0", desc: "Turns sound on and off" });

What am I doing wrong please?

knadh commented 9 years ago

The snippet looks fine. Can you share the rest of the code? (DB initialisation and population)

On Saturday 30 May 2015, danbuntu notifications@github.com wrote:

I'm trying to use insertOrUpdate and I'm getting the error:

Cannot read property 'toString' of null

I'm using the following code;

lib.insertOrUpdate("settings", {name: 'sound'}, { name: "sound", gameState: "0", desc: "Turns sound on and off" });

What am I doing wrong please?

— Reply to this email directly or view it on GitHub https://github.com/knadh/localStorageDB/issues/53.

danbuntu commented 9 years ago

the full code for the block looks lile:

var lib = new localStorageDB("carc", localStorage), existsSettings = lib.tableExists("settings"),

if (existsSettings === false) { lib.createTable("settings", ["name", "gameState", "desc"]); lib.insert("settings", {name: "gamestate", gameState: "maingame", desc: "sets the current game state"}); lib.insert("settings", {name: "slidertype", gameState: "0", desc: "sets slider type 0 = slider 1 = buttons"}); lib.insert("settings", {name: "sound", gameState: "0", desc: "Turns sound on and off"}); lib.commit(); } else { // check for sound in the setting table and add if needed lib.insertOrUpdate("settings", {name: 'sound'}, { name: "sound", gameState: "0", desc: "turns sound on and off" }); lib.commit();

    }
knadh commented 9 years ago

existsSettings = lib.tableExists("settings"),

^ this statement has to terminate with a semi column, yes?

On Saturday 30 May 2015, danbuntu notifications@github.com wrote:

the full code for the block looks lile:

var lib = new localStorageDB("carc", localStorage), existsSettings = lib.tableExists("settings"),

if (existsSettings === false) { lib.createTable("settings", ["name", "gameState", "desc"]); lib.insert("settings", {name: "gamestate", gameState: "maingame", desc: "sets the current game state"}); lib.insert("settings", {name: "slidertype", gameState: "0", desc: "sets slider type 0 = slider 1 = buttons"}); lib.insert("settings", {name: "sound", gameState: "0", desc: "Turns sound on and off"}); lib.commit(); } else { // check for sound in the setting table and add if needed lib.insertOrUpdate("settings", {name: 'sound'}, { name: "sound", gameState: "0", desc: "turns sound on and off" }); lib.commit();

}

— Reply to this email directly or view it on GitHub https://github.com/knadh/localStorageDB/issues/53#issuecomment-107065540 .

danbuntu commented 9 years ago

sorry yes bad cut and paste on my part - that bit is correct in my code - I have a whole bunch of other tableexists calls in the function but I removed them when pasting here to simplify it

knadh commented 9 years ago

The problem must be elsewhere. I tested the following and it worked fine.

var lib = new localStorageDB("carc", localStorage),
    existsSettings = lib.tableExists("settings");

if (existsSettings === false) {
    lib.createTable("settings", ["name", "gameState", "desc"]);
    lib.insert("settings", {name: "gamestate", gameState: "maingame", desc: "sets the current game state"});
    lib.insert("settings", {name: "slidertype", gameState: "0", desc: "sets slider type 0 = slider 1 = buttons"});
    lib.insert("settings", {name: "sound", gameState: "0", desc: "Turns sound on and off"});
    lib.commit();
} else {
    // check for sound in the setting table and add if needed
    lib.insertOrUpdate("settings", {name: 'sound'}, {
        name: "sound",
        gameState: "0",
        desc: "turns sound on and off"
    });
    lib.commit();
}
console.log(lib.queryAll("settings"));
danbuntu commented 9 years ago

hmm blast. I'll keep digging - thanks for your time