我正在尝试使用以下代码创建 LBUser 的用户扩展并将其保存到服务器:
LBRESTAdapter *adapter = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).adapter;
if (adapter) {
TeacherRepository *repo = (TeacherRepository *)[adapter repositoryWithClass:[TeacherRepository class]];
if (repo) {
Teacher *st = (Teacher *)[repo createUserWithEmail:@"test@test.com" password:@"test"];
st.bigarea = @"Mathematics";
NSLog(@"Email: %@", st.email);
NSLog(@"Password: %@", st.password);
if (st) {
[st saveWithSuccess:^{
NSLog(@"Saved in server!");
} failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
}
}
}
TeacherRepository 是一个 LBUserRepository 扩展。显然用户的创建是好的,但是当我尝试保存用户时,我得到了这个错误:
Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 422" UserInfo=0x7f9418f07db0 {NSLocalizedRecoverySuggestion={"error":{"name":"ValidationError","status":422,"message":"The `Teacher` instance is not valid. Details: `password` can't be blank; `email` can't be blank; `email` is blank.","statusCode":422,"details":{"context":"Teacher","codes":{"password":["presence"],"email":["presence","format.blank"]},"messages":{"password":["can't be blank"],"email":["can't be blank","is blank"]}},"stack":"ValidationError: The `Teacher` instance is not valid. Details: `password` can't be blank; `email` can't be blank; `email` is blank.
似乎我的名为 Teacher 的 LBUser 扩展名无效,并且电子邮件和密码为空白,但根据 NSLog,它们不是空白的。这是Teacher.h:
#import <Foundation/Foundation.h>
#import <LoopBack/LoopBack.h>
@interface Teacher : LBUser
@property (strong,nonatomic) NSString *bigarea;
@end
回到服务器,我有一个 .json 将老师描述为:
{
"name": "Teacher",
"plural": "Teachers",
"base": "User",
"properties": {
"bigarea": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"courses": {
"type": "hasMany",
"model": "Course",
"foreignKey": "courseId"
}
},
"acls": [],
"methods": []
}
此外,我对如何表示 Objective C 中的关系感到有些困惑。有人可以给我一点启示吗?