Reputation: 81
I would like to get user and filter whether the given username exists. I have problem with property
'filter' does not exist on type 'Object'
TakeUsernameIfExist(user: User) {
return this.http.get(this.rootUrl + 'api/TakeUserName/' + user.UserName )
.delay(1000)
.map(users => users.filter( data => data === user.UserName))
.map(users => !users.length);
}
I added import for filter and the problem is still.
import 'rxjs/add/operator/filter'; My Web API method:
[HttpGet]
[AllowAnonymous]
[Route("api/TakeUserName/{username}")]
public string TakeUserName(string username)
{
var userStore = new UserStore<ApplicationUser>(new ApplicationDbContext());
var manager = new UserManager<ApplicationUser>(userStore);
var result = userStore.FindByNameAsync(username);
if(result==null)
{
return null;
}
else
{
return result.Result.UserName;
}
}
Any help or suggestion is welcome.
Upvotes: 2
Views: 148
Reputation: 3778
Your problem is you're NOT returning a LIST (or Array ) from you WEBAPI BUT you're return an OBJECT.. and Filter JS method works on ARRAY
so here you don't need the filter method cause you're searching for username (and you can't have more than one user in Identity Asp.NET with 2 similar username )
so try this in your WEB API return a bool:
[HttpGet]
[AllowAnonymous]
[Route("api/TakeUserName/{username}")]
public async<IHttpActionResult> TakeUserName(string username) //<-- IHttpActionResult is better
{
var userStore = new UserStore<ApplicationUser>(new ApplicationDbContext());
var manager = new UserManager<ApplicationUser>(userStore);
var result =await userStore.FindByNameAsync(username);
return (result==null || !result.IsSuccess //<-- not sure for this prorperty name) ? Ok(false) : Ok( !string.isNullOrEmtpy(result.UserName);
}
so in your client:
TakeUsernameIfExist(user: User) : Promise<bool> {
return this.http.get<bool>(this.rootUrl + 'api/TakeUserName/' + user.UserName ).toPromise()
}
Hope it helps you!
Upvotes: 1