错误TypeError:每当我尝试通过ID来获取用户时,都无法读取未定义的属性“ id”

时间:2019-11-08 02:28:48

标签: angular typescript https undefined

我正在尝试通过ID搜索用户,但不能。 每次我调用该函数来获取用户时,都会使用户空白,并且我想不出解决此问题的方法。

我的功能:

getCandidate(candidate: Candidate) {
    this.candidateService.getCandidateById(candidate.id).subscribe( resp => {
      this.dataCandidate = resp;
    })
  }

在函数中,我调用的是候选人。id,但由于候选人为空而返回错误

我的模特:

export class Candidate {
    id: number;
    full_name: string;
    cpf: string;
    rg: number;
    birth_date: string;
    phone: string;
    username: string;
    email: string;
    password: string;
}

我的GET:

const apiUrl = 'https://delineaapi.herokuapp.com:443/candidate/';
getCandidateById(id: number): Observable<Candidate> {
    const url = `${apiUrl}${id}`;
    return this.http.get<Candidate>(url)
  }

如果您输入console.log (candidate),则响应为空

我还有另一个函数可以返回用户列表,而这个函数运行良好。

0 个答案:

没有答案