1. #1
    Sencha User
    Join Date
    Feb 2012
    Posts
    10
    Vote Rating
    0
    miraclemaker is on a distinguished road

      0  

    Default Answered: Exception when trying to read an empty store

    Answered: Exception when trying to read an empty store


    Hi guys I have an application that stores locations to a store. When it starts up I want to check if the user has any locations stored, I'm doing this as follows:

    Code:
        launch: function() {
            
            //create store ref
            var store = Ext.create('Ext.data.Store', {
                model: 'MyApp.model.Location'
            });
            
            //do we have at least one item in the store?
            MyApp.model.Location.load(1, {
                success: function(location) {
                    console.log("Got an item");
                },
                failure: function(record) {
                    console.log("Failed to retrieve item");
                }
            });
        }
    Basically I'm trying to load the first item and if it is successful do one thing, otherwise do something else. When there is data in the store this works fine, but when the store is empty I get an Exception:

    Uncaught TypeError: Cannot read property 'id' of null

    any tips on a better way of doing this?

    Thanks!

  2. You should be looking at the store to see if there is the record in it, not load a model instance.

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,144
    Answers
    3506
    Vote Rating
    855
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    You should be looking at the store to see if there is the record in it, not load a model instance.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User
    Join Date
    Feb 2012
    Posts
    10
    Vote Rating
    0
    miraclemaker is on a distinguished road

      0  

    Default


    Perfect - many thanks mitchellsimoens.

Thread Participants: 1

Tags for this Thread