在阅读Handling Errors with Ember Data以及如何解决Ember JS中的错误处理的众多其他提示和技巧后,我仍然无法弄清楚我的代码有什么问题。
首先,我有一个像这样的用户模型:
import DS from 'ember-data';
export default DS.Model.extend({
firstName: DS.attr('string'),
surName: DS.attr('string'),
email: DS.attr('string'),
plainPassword: DS.attr('string')
});
显然我没有从服务器返回密码,但我认为模型中需要从注册表单发送密码。无论如何,继续。
表单模板如下所示:
<!-- start Register -->
<div class="container-fluid">
<form name="form_register" {{action 'submit' on="submit"}} class="form-register" novalidate="novalidate">
<div class="col-md">
<label class="el-input-wrap">
First Name
{{input class="el-input" placeholder='firstName' value=model.firstName}}
</label>
<label class="el-input-wrap">
Last Name
{{input class="el-input" placeholder='surName' value=model.surName}}
</label>
</div>
<div class="col-md">
<label class="el-input-wrap">
Email
{{input class="el-input" placeholder='email' value=model.email}}
</label>
<label class="el-input-wrap">
Password
{{input class="el-input" placeholder='password' value=model.plainPassword type='password'}}
</label>
</div>
<div class="col">
<label class="el-checkbox">
By registering I agree to the terms and conditions
</label>
</div>
<button type="submit" class="el-btn mod-full-w">REGISTER</button>
</form>
</div>
<!-- end Register -->
接下来,我有一个处理注册表的注册控制器:
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
cancel() {
this.get('model').deleteRecord();
return true;
},
submit() {
var user = this.get('model');
user.save().then(
() => this.transitionToRoute('register')
).catch((adapterError) => {
console.log(adapterError);
});
}
}
});
在这里,我尝试过console.log错误,errors.firstName等等。但没有错误显示。
adapterError告诉我错误格式不是JSON API格式。但从我所看到的,它是:
{
"errors": {
"source": {
"pointer": "user\/email"
},
"detail": "user.email.not_blank"
}
}
最后,我在adapters / application.js中使用RESTAdapter:
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
export default DS.RESTAdapter.extend(DataAdapterMixin, {
namespace: 'app_dev.php/api',
authorizer: 'authorizer:token', //or authorizer: 'authorizer:jwt'
});
对于我的生活,我无法弄清楚出了什么问题。我非常感谢任何帮助或指示。谢谢。
答案 0 :(得分:2)
想出来。简直就是我没有返回错误数组。正确的错误响应是:
{
"errors": [{
"source": {
"pointer": "user\/firstName"
},
"detail": "user.first_name.not_blank"
}, {
"source": {
"pointer": "user\/surName"
},
"detail": "user.sur_name.not_blank"
}, {
"source": {
"pointer": "user\/email"
},
"detail": "user.email.not_blank"
}]
}