aaronpowell / db.js

db.js is a wrapper for IndexedDB to make it easier to work against
http://aaronpowell.github.com/db.js/
MIT License
818 stars 142 forks source link

code example Error #94

Closed tinamore closed 8 years ago

tinamore commented 9 years ago

Hi. I've created a code example, but an error occurred. Uncaught TypeError: Cannot read property 'people' of undefined

var server;
db.open( {
    server: 'my-app',
    version: 1,
    schema: {
        people: {
            key: { keyPath: 'id' , autoIncrement: true },
            // Optionally add indexes
            indexes: {
                firstName: { }
                /* answer: { unique: true } */
            }
        }
    }
} ).then( function ( s ) {
    server = s
} );

server.people.add( {
    firstName: 'Aaron',
    lastName: 'Powell',
    answer: 1
} ).then( function ( item ) {
    console.log (item);
} );

You have the html source file for example ? I do not understand/tests/specs. Thank you very much

aaronpowell commented 9 years ago

You need to wait until the database is open before you can query it. The open method is asynchronous and returns a Promise. Until that Promise is fulfilled (ie the .then method executes) the server variable is undefined.

tinamore commented 9 years ago

Thank you very much for your answer

I enjoyed your db.js than all the Wrapper for IndexedDB, because it's very compact, gently. Actually I do not know how to use Promise :(. Can you give me a complete example for creating a database and add a row data? As you said, I must to check the database has been created or not? before making the add, delete, update. check the database was created or yet to be repeated many times ?, because the source I used a lot more action, delete, update?

brettz9 commented 8 years ago

You just need to move your add code into the then where server will be sure to be ready:

db.open( {
    server: 'my-app',
    version: 1,
    schema: {
        people: {
            key: { keyPath: 'id' , autoIncrement: true },
            // Optionally add indexes
            indexes: {
                firstName: { }
                /* answer: { unique: true } */
            }
        }
    }
} ).then( function ( server ) {
    // We can use `server` now, as it is ready
    server.people.add( {
        firstName: 'Aaron',
        lastName: 'Powell',
        answer: 1
    } ).then( function ( item ) {
        console.log (item);
    } );
} );
// Code here will execute before the `then` and before the server is ready;
//  to wait for it to be ready, you must put code inside the `then` above as I
//  have done

It will only reach the then code if the database has been created, so no need to do separate checks.

You can check for errors by adding a second function to the "then" (or adding a new function catch)

somePromise.then(function () {
    // Handle success here
}).catch(function () {
    // Handle errors here
});

Or you can do this:

somePromise.then(function () {
    // Handle success here
}, function () {
    // Handle errors here
});

somePromise might be db.open or server.people.add or whatever.

brettz9 commented 8 years ago

You can read about Promises at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise . You might also learn from the latest db.js docs and tests.

brettz9 commented 8 years ago

I think this issue can be closed now that answers were provided.