How to display error message in ember js 2.0

那年仲夏 提交于 2019-12-10 18:06:08

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/36572090/how-to-display-error-message-in-ember-js-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!