我想获取用户并过滤给定的用户名是否存在。我的财产有问题
“对象”类型上不存在“过滤器”
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);
}
我为过滤器添加了导入,问题仍然存在。
导入'rxjs / add / operator / filter'; 我的Web API方法:
[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;
}
}
欢迎任何帮助或建议。
答案 0 :(得分:1)
您的问题是您没有从WEBAPI返回列表(或Array),但返回了对象。.并且Filter JS方法在ARRAY上有效
因此,这里不需要过滤方法,因为您正在搜索用户名(并且在Identity Asp.NET中不能有多个用户使用2个相似的用户名)
因此请在您的WEB API中尝试此操作,以返回布尔值:
<?php
// Input specified as string for the sake of this demo.
$inputData = <<<EOT
XYZ - - -
Day1 12 28 MCS
Day2 15 30 PCS
Day3 18 28 LH
Day4 11 26 SUN
Day5 20 34 RA
Day6 16 26 PCS
Day7 6 15 FO
EOT;
// Split string on EOL character, into array.
$input = explode(PHP_EOL, $inputData);
// Or, if you're reading from a file, comment everything above and uncomment the following line:
// $input = file('Data/data/XYZ.txt');
// $input is now an array, and allows us to iterate over it using foreach.
// $rowIndex will hold the array index, $line will hold a single line from the input.
foreach ($input as $rowIndex => $line)
{
// Skip header.
if ($rowIndex == 0)
continue;
// Trim any unwanted leading whitespace, so the resulting array starts with a usable value.
// preg_split() will split the line on whitespace, and return an array of all column values.
$cols = preg_split('/[\s]+/', trim($line));
// You can now read out the array of column values however you see fit.
$v1 = $cols[0];
$v2 = $cols[1];
$v3 = $cols[2];
$v4 = $cols[3];
echo "v1=$v1 v2=$v2 v3=$v3 v4=$v4<br>\n";
}
所以在您的客户中:
[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);
}
希望对您有帮助!