打字稿:从HttpClient.Post获取未定义的<t>

时间:2017-12-04 10:26:09

标签: angular typescript

我是新手来输入脚本,我不确定我在这里做错了什么:

  async login(credentials: Credentials): Promise<boolean> {
    let res: AuthToken = await this.httpClient.post<AuthToken>(this.endpoint + '/session/login', JSON.stringify(credentials), { headers: this.headers }).toPromise();
    console.log(res);
    console.log(res.auth_token);
    return true;
  }

如果我通过等待来自我的登录组件来调用此方法,我在控制台输出中看到 res 变量是一个有效的 AuthToken 响应:

{
  "id": "73113e63-df5e-4e4e-bde4-29108ddaaafe",
  "auth_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0QGV4YW1wbGUuY29tIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvZW1haWxhZGRyZXNzIjoidGVzdEBleGFtcGxlLmNvbSIsInVuaXF1ZV9uYW1lIjoidGVzdEBleGFtcGxlLmNvbSIsImp0aSI6IjBjYjY3NTcwLWYxNWEtNDBmNy1iNGEzLWI5ODM2Y2M2M2Y1OSIsImlhdCI6MTUxMjM4MjA3Mywicm9sIjoiYXBpX2FjY2VzcyIsImlkIjoiNzMxMTNlNjMtZGY1ZS00ZTRlLWJkZTQtMjkxMDhkZGFhYWZlIiwidGVuYW50SWQiOiI1ZDg2N2NiNS1kMmI5LTQxMjUtYWZjZC0wYjI5ZjRlZWQxY2QiLCJuYmYiOjE1MTIzODIwNzMsImV4cCI6MTUxMjQyNTI3MywiaXNzIjoiVHJpZG9vIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwLyJ9.7cN74Adh5d2gAu4-R2fffvEhrLWuAKc6QxKEZZfi09E",
  "expires_in": 43200
}

但如果我尝试访问 res.auth_token ,我会 undefined

我在这里做错了什么?

authtoken.interface.ts
export interface AuthToken {
    id: string;
    auth_token: string;
    expires_in: number;
}

0 个答案:

没有答案