3

I would like to display an error message when the server responses with record not found.

The model in the route handler:

model: function(userLoginToken) {
    var userLoginToken= this.store.createRecord('userLoginToken');
    return userLoginToken;
},

The action:

actions: {

  sendOTP: function(userLoginToken) {

    var thisObject = this;
    var model=this.currentModel;

    this.store.findRecord('user-login-token', userLoginToken.get('mobileNumber')).then(function(response) {
        //thisObject.get('controller').set('model', response);
      },
      function(error) {
        //thisObject.get('controller').set('model', error);
        //alert("model======== "+model.get('errors'));
      }); 
    },

The template is not displaying any error message.

The template:

{{#each model.errors.messages as |message|}}
  <div class="errors">
    {{message}}
  </div>
{{/each}}

Unfortunately, the error message doesn't appear.

ykaragol
  • 6,139
  • 3
  • 29
  • 56
  • Hi. Have you uploaded your project to github? Maybe you can share more code with us there. Could you please share some server response payload, which adapter do you use? – Zoltan Apr 15 '16 at 07:31

2 Answers2

2

Ember depends on an DS.error object, in order to get errors from your models the response has to fulfill the requirements. In order to get Ember to recognize an valid error, in Ember 2.x the error code MUST be 422 and has to follow jsonapi http://jsonapi.org/format/#errors-processing

1

If you want to catch the errors from the backend response you have to use the catch method:

this.store.findRecord('user-login-token', userLoginToken.get('mobileNumber'))
  .then(success => {
    // Do whatever you need when the response success
  })
  .catch(failure => {
    // Do whatever you need when the response fails
  })
},

For catching the errors automatically as you are doing in your template, your backend needs to response in the right way. I would suggest you to read the answer for this SO question.

Community
  • 1
  • 1
Henry Vonfire
  • 1,281
  • 10
  • 18
  • Its not working still error message is not displaying in the template .error message is not set in the model . because in ember data model it is showing a 0 length in error property. – Nidhi Saharawat Apr 13 '16 at 04:36
  • 1
    As I told you, there are two ways of catching the errors: one manually by using the `catch` method and the other one automatically using the code you have in your `template`. If you choose the second option, you need to build the payload sent from the server in a specific way for `ember-data` to understand it automatically and fill the error property. It would be helpful if you provide us the response of the server and tell us which adapter are you using. – Henry Vonfire Apr 13 '16 at 07:52