为什么使用Effect(ngrx/store)获取null值



问题

res=无效

除了从Effect获得响应之外,一切似乎都在工作。我可以从authcontroller返回identity,并在创建用户时成功保存。因为我得到了空响应,所以我不能再执行步骤this.store.dispatch(new UserCreated({ user: res }));。我错过了什么?

http服务

createUser(user: User): Observable<User> {
var userRoles = user.roles.map(x=>x.name);   
const body = {
username: user.username,
password: user.password,
firstname: user.firstname,
lastname: user.lastname,
roles: userRoles,
email: user.email,
phonenumber:user.phonenumber,
};
const httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Type", "application/json");
return this.http
.post<User>(API_CREATE_USER_URL, body,{headers:httpHeaders});
}

效果

@Effect()
createUser$ = this.actions$
.pipe(
ofType<UserOnServerCreated>(UserActionTypes.UserOnServerCreated),
mergeMap(( { payload } ) => { 
debugger;
this.store.dispatch(this.showActionLoadingDistpatcher);
return this.auth.createUser(payload.user).pipe( 
tap(res => {
debugger;
this.store.dispatch(new UserCreated({ user: res }));
})
);
}),
map(() => {
return this.hideActionLoadingDistpatcher;
}),
);

asp.net核心账户控制器

[HttpPost]
public async Task<IActionResult> Post(RegistrationViewModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var userIdentity = _mapper.Map<AppUser>(model);
//var userIdentity = new AppUser();
//userIdentity.Email = model.Email;  
//userIdentity.UserName = model.UserName;

//var role = _roleManager.FindByNameAsync(model.RoleName).Result;
//await _userManager.AddToRoleAsync(userIdentity, role.Name);
var result = await _userManager.CreateAsync(userIdentity, model.Password);
if (!result.Succeeded)
{
return new BadRequestObjectResult(Error.AddErrorsToModelState(result, ModelState));
}
await _userManager.AddToRolesAsync(userIdentity, model.Roles);
await _appDbContext.SaveChangesAsync(); 
return Ok(userIdentity);
}

前端部分是正确的。您的后端似乎没有返回创建的用户。

我建议您在向API_CREATE_USER_URL发送请求后,在开发人员工具中检查网络活动。

相关内容

  • 没有找到相关文章

最新更新