s.webbandit
s.webbandit

Reputation: 17000

How to get response message from server on Store.sync()

I have a store which I'd like to sync() with server.

Store.sync() method have success and failure property-functions, they have Ext.data.Batch and options as parameters.

If I get such response from server:

{
  success: false,
  msg: "test error!"
}

failure method invokes.

How to access response msg property from within failure method?

Upvotes: 2

Views: 5698

Answers (2)

babinik
babinik

Reputation: 648

Store.sync() method have success and failure property-functions, they have Ext.data.Batch and options as parameters.

Any failed operations collecting inside batch.exceptions array. It has all what you need. Just go through the failed operations, processing them.

Operation failure message ("test error!") would be inside - operation.error

store.sync({
    failure: function(batch, options) {
        Ext.each(batch.exceptions, function(operation) {
            if (operation.hasException()) {
                Ext.log.warn('error message: ' + operation.error);
            }
        });
    }
});

Proxy set it inside processResponse method:

operation.setException(result.message);

Where:

  • Operation's setException method sets its error property (error message there).
  • result is Ext.data.ResultSet and the result.message if filled by the proxy's reader, using the reader's messageProperty

But, by default reader's message property is messageProperty: 'message'. In your case you should configured the reader with correct messageProperty, like:

reader: {
    ...
    messageProperty: 'msg'
}

or return from the server response with metadata configuration property, like:

{
    "success": false,
    "msg": "test error!", 
    "metaData": {
        ...
        "messageProperty": 'msg'
    }
}

Json reader - Response MetaData (docs section)

Upvotes: 2

Vlad
Vlad

Reputation: 3645

    store.sync({
        failure: function(batch, options) {
            alert(batch.proxy.getReader().jsonData.msg);
        }
    });

Upvotes: 6

Related Questions