简体   繁体   中英

How to display error message in ember js 2.0

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.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM