简体   繁体   English

如何在ember js 2.0中显示错误消息

[英]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. Ember依赖于DS.error对象,为了从模型中获取错误,响应必须满足要求。 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 为了让Ember能够识别出有效的错误,在Ember 2.x中,错误代码必须是422并且必须遵循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: 如果要捕获后端响应中的错误,则必须使用catch方法:

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 . 我建议你阅读这个问题的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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