简体   繁体   中英

Angular 2 and TypeScript Promise return array

I am trying to return an array from a service using Promise.Then. The function is my service is follow:

userList: Users[] = [];

getUsers = {
    userList: (): Promise<Users[]> => {
        let headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded');
        headers.append('Authorization', 'Bearer ' + this.authenticationService.token);
        return this.http.get(this.authenticationService.url + '/tournament/getGames', {
            headers: headers
        }).map((response: Response) => {
            var status = response.json().status;
            console.log(response.json().status);
            if (status) {                   
                this.userList = response.json().users;
                console.info(response.json().users);
                return this.userList;
            }
            else {
                return null;
            }
        }).toPromise();

    }
}

then I am using this in administrator.component.ts to get the Users list:

usersList: Users[] = [];

inside ngOnInit:

this.getData().then(() => this.isDataAvailable = true);

getData function:

 getData(): Promise<Users[]> {
    return this.UserService.getUsers().then(users => {
        this.usersList= users;
        console.log(this.usersList);
    });
}

and the code is returning this error message:

cannot invoke an expression whose type lacks a call signature

Any help is much appreciated as I use a similar logic in a different component.

In your service getUsers is not a function. getUsers.userList is the function. So you should call it with this.UserService.getUsers.userList() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM