断言失败:`AdapterError`期待json-api格式化的错误数组 [英] Assertion Failed: `AdapterError` expects json-api formatted errors array
问题描述
阅读使用Ember Data处理错误以及许多关于如何解决Ember JS中的错误处理的提示和技巧,我仍然无法弄明白我的代码有什么问题。
After reading Handling Errors with Ember Data and numerous other tips and tricks on how to solve error handling in Ember JS, I still am not able to figure out what is wrong with my code.
对于初学者,我有一个如下所示的用户模型:
For starters, I have a user model like so:
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')
});
显然,我没有从服务器返回密码,但我认为在模型中需要从注册表发送。无论如何,向前。
Obviously I do not return a password from the server, but I assume it is needed in the model to send it from the registration form. Anyway, onward.
表单模板如下所示:
<!-- 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 -->
接下来我有注册控制器处理注册表单:
Next I have the registration controller that handle the registration form:
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等。但是没有显示错误。
Here, I have tried to console.log errors, errors.firstName and so on. But no errors show.
adapterError告诉我错误格式不是JSON API格式。但是从我可以看到的是:
The adapterError tells me the error format is not in JSON API format. But from what I can see, it is:
{
"errors": {
"source": {
"pointer": "user\/email"
},
"detail": "user.email.not_blank"
}
}
最后,我在适配器/ application.js中使用RESTAdapter:
Lastly, I am using the RESTAdapter in adapters/application.js:
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'
});
对于我的生活,我无法弄明白什么是错误的。我非常感谢任何帮助或指点。谢谢。
For the life of me, I can not figure out what is wrong. I would very much appreciate any help or pointers. Thank you.
推荐答案
只是我没有返回错误数组。正确的错误响应是:
Figured it out. Is was simply that I didn't return an errors array. The correct error respons was:
{
"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"
}]
}
这篇关于断言失败:`AdapterError`期待json-api格式化的错误数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!