Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检索到的mysql数据库对象中的字段都未定义_Java_Angular_Spring Boot_Spring Data Rest - Fatal编程技术网

Java 检索到的mysql数据库对象中的字段都未定义

Java 检索到的mysql数据库对象中的字段都未定义,java,angular,spring-boot,spring-data-rest,Java,Angular,Spring Boot,Spring Data Rest,我正在尝试用angular 4创建一个登录组件。在后端Spring引导中,使用Spring数据Rest和MySQL数据库。当我想用检索到的帐户的登录凭据检查提交的登录详细信息时,我无法这样做,因为检索到的帐户对象中的所有字段都未定义,而对象本身未定义(我用if结构检查)。有人能看看我的代码,告诉我问题出在哪里吗?我将从angular 4类和服务开始,然后是account pojo和Spring REST存储库 四类: import {Person} from "./Person"; import

我正在尝试用angular 4创建一个登录组件。在后端Spring引导中,使用Spring数据Rest和MySQL数据库。当我想用检索到的帐户的登录凭据检查提交的登录详细信息时,我无法这样做,因为检索到的帐户对象中的所有字段都未定义,而对象本身未定义(我用if结构检查)。有人能看看我的代码,告诉我问题出在哪里吗?我将从angular 4类和服务开始,然后是account pojo和Spring REST存储库

四类:

import {Person} from "./Person";
import {Role} from "./Role";
export class Account {
  id: number;
  username: string;
  password: string;
  person: Person;
  roles: Role[];

}

export class LaborPeriod{
    id: number
    beginDate: Date
    endDate: Date
    hours: number
}

import {LaborPeriod} from "./LaborPeriod";
export class Person{
    id:number
    name:string
    laborPeriods:LaborPeriod[]
}


export class Role{
    id: number
    name: string
}
登录组件类:

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'],
  providers: [AccountService]
})
export class LoginComponent {
  title: string;
  loginForm: FormGroup;

  constructor(private fb: FormBuilder,
              private accountService: AccountService,
              private router: Router) {
    this.title = "Inloggen";
    this.loginForm = fb.group({
      email: ['', [Validators.required, Validators.email]],
      password: ['', Validators.required]
    });
  }

  onSubmit() {
    const formModel = this.loginForm.value;

    const account: Account = this.accountService.authenticate(formModel.email, formModel.password);
    console.log(account.id + " " + account.password + " " + " " + account.username)
    console.log(account !== undefined)
    // if (account !== undefined) {
    //   this.router.navigate(["account/update", account.id]);
    // }
    // else{/*username or password doesent exist*/}
  }
}
会计服务中的相关方法

  authenticate(username: string, password: string): Account {
    return this.restapi.postCredentials(username, password);
  }
restApi:

@Injectable()
export class RestApiService {
  apiUrl: string = "/api/accounts";
  apiPostUrl: string = "api/accounts/search/findByUsernameAndPassword";
  data: Account;


  constructor(private http: Http) {}

  getData(){
    return this.http.get(this.apiUrl).map((res: Response) => res.json())
  }

  getContacts(){
    this.getData().subscribe(data=> {console.log(data); this.data = data})
  }

  postCredentials(username:string, password:string): Account {
    let params = new URLSearchParams();
    params.append("username", username);
    params.append("password", password);
    // params.set("username", "henk@gmail.com");
    // params.set("password", "12345");
    this.http.get(this.apiPostUrl, {params: params}).map((res: Response) => res.json()).subscribe(data=> { this.data = data});
    console.log(this.data);
    return this.data;
  }

}
现在是后端的一切

@Entity
public class Account {
    @Id
    @GeneratedValue
    private Long id;
    private String username;
    private String password;
    @ManyToMany(cascade = CascadeType.ALL)
    private List<Role> roles;
    @OneToOne(cascade = CascadeType.ALL)
    private Person person;

@Entity
public class LaborPeriod {
    @Id
    @GeneratedValue
    private Long id;
    private Instant beginDate;
    private Instant endDate;
    private Integer hours;

@Entity
public class Role {
    //
    @Id
    @GeneratedValue
    private Long id;
    private String name;

@Entity
public class Person {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    @OneToMany(cascade = CascadeType.ALL)
    private Collection<LaborPeriod> laborPeriods;
@实体
公共类帐户{
@身份证
@生成值
私人长id;
私有字符串用户名;
私有字符串密码;
@多个(级联=级联类型.ALL)
私有列表角色;
@OneToOne(级联=级联类型.ALL)
私人;
@实体
公共阶级劳动时期{
@身份证
@生成值
私人长id;
私人即时开始;
私人即时终止日期;
私人整数小时;
@实体
公共阶级角色{
//
@身份证
@生成值
私人长id;
私有字符串名称;
@实体
公共阶层人士{
@身份证
@生成值
私人长id;
私有字符串名称;
@OneToMany(级联=级联类型.ALL)
私人收藏;
帐户存储库

@RepositoryRestResource
public interface AccountRepository extends JpaRepository<Account, Long> {
    Account findByUsername(@Param("username") String username);
    Account findByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
}
@RepositoryRestResource
公共接口AccountRepository扩展了JpaRepository{
帐户findByUsername(@Param(“用户名”)字符串用户名);
帐户findByUsernameAndPassword(@Param(“用户名”)字符串用户名,@Param(“密码”)字符串密码);
}

非常感谢您的帮助。

使用
界面
而不是课堂,并键入您的回答,如下所示:

getData(){
    return this.http.get(this.apiUrl).map((res: Response) => <Account> res.json())
  }
getData(){
返回this.http.get(this.apiUrl).map((res:Response)=>res.json())
}

注意:如果您使用类,您应该有一个构造函数来设置属性。

您需要从post请求返回一个可观察的值。这是异步的,因此您的响应可以在回调中获得。即使您尝试从回调
订阅
,也可以是不可能的,因为这也是异步的。所以您需要从post请求返回一个可观察的:

return this.http.get(this.apiPostUrl, {params: params})
  .map((res: Response) => res.json())
在这里,您实际上可以跳过
authenticate
方法,我看不出它在这里对您有什么用处。但是如果您想保留它,当然可以:)

因此,您接下来要做的是在组件中订阅此post请求

constructor(private restApiService: RestApiService /** **/) { }

onSubmit() {
  const formModel = this.loginForm.value;
  this.restApiService.postCredentials(formModel.email, formModel.password);
    .subscribe(.....)
}

值得一读:

您好,为什么我的account类需要一个构造函数?我不能简单地使用point(.)操作符来设置字段吗?(例如account.username='test'。您可以,但您需要实例化对象。它与提供的答案有什么关系?是否符合您的需要?:)是的,我工作了。谢谢!太好了,很高兴听到!))