我有一个.NET Core2.0应用程序,在该应用程序中,注册新用户时返回验证错误,如下所示:
var existingUser = await _userManager.FindByEmailAsync(model.Email);
{
if (existingUser != null)
{
return BadRequest(new IdentityError()
{
Description = "This email address has already been registered."
});
}
}
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
return new JsonResult(result);
}
return BadRequest(result.Errors.ToList());在我的Angular 5应用程序中,我有以下代码:
this.userService.register(this.model)
.finally(() => this.isRequesting = false)
.subscribe(
result => {
if (result) {
this.alertService.success('Registration successful', '', false, true);
this.router.navigate(['/login']);
}
},
error => {
console.log(error)
this.alertService.error(error, 'Registration failed', false, false);
});我的console.log(error)行显示如下:

如何解析JSON以提取“description”字段并在它们周围加上段落标记?
发布于 2018-02-10 20:18:26
您已经有了一个已解析的JSON。您需要访问error属性。error是一个数组,错误在它的项中。因此,您可以只访问error[0].code或error[0].description。
console.log(error.error[0].description)对于许多错误
for(let e of error.error) {
console.log(e.description);
}发布于 2018-02-10 21:03:12
您可以使用索引访问错误,
console.log(error.error[0].description);在具有多于一个错误使用的情况下
for (let erorObj in error.error) {
console.log(errorObj);
}https://stackoverflow.com/questions/48720658
复制相似问题