0
我是新來對付我已經開發出一種服務REST API返回我JSON字符串角2。提出請求的是下面的代碼:我如何能夠將Promise變成類似列表的東西?
getUsuaris(): Promise<Usuari> {
return this.http.get(this.URLUsuaris)
.toPromise()
.then(response => response.json().data as Usuari[])
.catch(this.handleError);
}
下面的代碼是一個簡單的登錄,沒有更多的,但不是因爲authenticatedUserList原因是NULL和obiamente其屬性。
private URLUsuaris = 'http://localhost:50541/api/Usuaris';
constructor(
private _logger: Logger,
private _router: Router,
private http: Http) { }
logIn(usuari: Usuari) {
var authenticatedUserList: Promise<Usuari> = this.getUsuaris();
var authenticatedUser: Usuari;
this._logger.log(authenticatedUserList[0]);
for (var i in authenticatedUserList) {
if (authenticatedUserList[i].usuari === usuari.Usuari1) {
authenticatedUser = authenticatedUserList[i];
break;
}
}
if (authenticatedUser && authenticatedUser.Password === usuari.Password){
localStorage.setItem("usuari", authenticatedUser.Usuari1);
this._router.navigate(['Home']);
this._logger.log(localStorage.getItem("usuari"));
this._logger.log('Usuari Registart!');
return true;
}
this._logger.log('Usuari No Registart!');
return false;
}
帶有REST API的服務器正確返回信息!
用戶類,這是什麼發送API:
export class Usuari {
public Id: number;
public Usuari1: string;
public Password: string;
constructor(usuari: string, password: string) { }
}
請,如果您有任何想法可能失敗的解釋沒有義務滿足未來同樣的錯誤。
非常感謝你!