Results 1 to 6 of 6

Thread: Store reader handling empty response

  1. #1
    Sencha User tf.alves's Avatar
    Join Date
    Feb 2010
    Location
    Portugal
    Posts
    54
    Answers
    3
    Vote Rating
    0
      0  

    Default Answered: Store reader handling empty response

    Hello everyone,

    My server not always returns a JSON string, sometimes it returns nothing (depending on the amount of items it has at the moment). The problem comes when I have to handle the response... It throws an exception:
    Uncaught TypeError: Cannot read property 'length' of undefined
    How can I handle this empty responses, in order to just show the emptyMessage instead of throwing the exception?

    The store code is as follows:

    Code:
    App.stores.events_store = new Ext.data.Store({    model: 'EventItem',
        sorters: 'data',
        proxy: {
            type: 'ajax',
            url: 'calendar.php',
            reader: {
                type: 'tree',
                root: 'items'
            }
        }    
    });

    Edit: Ok, I know if I return an empty JSON like {"items":{}} it will work, but how about if the response is nothing ("")?

  2. Quote Originally Posted by tf.alves View Post
    Edit: Ok, I know if I return an empty JSON like {"items":{}} it will work, but how about if the response is nothing ("")?
    IMO, that is not a good idea for app development. At least change the status code to tell the Proxy that something went wrong but you should at least return something.

  3. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,641
    Answers
    3938
    Vote Rating
    1272
      0  

    Default

    Quote Originally Posted by tf.alves View Post
    Edit: Ok, I know if I return an empty JSON like {"items":{}} it will work, but how about if the response is nothing ("")?
    IMO, that is not a good idea for app development. At least change the status code to tell the Proxy that something went wrong but you should at least return something.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  4. #3
    Sencha User tf.alves's Avatar
    Join Date
    Feb 2010
    Location
    Portugal
    Posts
    54
    Answers
    3
    Vote Rating
    0
      0  

    Default

    So, you're saying that it's better to have the server replying with a '{"items":{}}' than not replying at all, right?

    Thanks

  5. #4
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,641
    Answers
    3938
    Vote Rating
    1272
      0  

    Default

    In my experience yes. Even if there is some error, I still return

    {success: false, error : 'Something went wrong'}
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  6. #5
    Sencha User tf.alves's Avatar
    Join Date
    Feb 2010
    Location
    Portugal
    Posts
    54
    Answers
    3
    Vote Rating
    0
      0  

    Default

    Thanks a lot, you're really helping me out

  7. #6
    Sencha User
    Join Date
    Dec 2016
    Location
    Pinar del Rio, Cuba
    Posts
    4
    Vote Rating
    0
      0  

    Default

    I know it's a bit late but anyway I'll ask.
    What about the unpredictable cases where the server returns an empty response, cero byte I mean? How can I prevent the store to mark records as saved? The only solution I can imagine but have not tested it yet, is to hook into global Ext.Ajax and try somehow to stop the event chain that ends up with the store thinking everything is OK and marking records as saved, synced.
    Thanks for your time.
    I asked here because I think it's the same question with just a small different detail.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •