为什么用Effect(ngrxstore)得到空值?

问题描述 投票:0回答:1

问题

在效果中,res = null

除了得到来自 Effect. 我可以回去了 identityauthcontroller 并在创建用户时成功保存。因为我得到的是null的响应,所以我不能做进一步的步骤。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);
}
asp.net-core angular8 ngrx asp.net-core-3.1 ngrx-effects
1个回答
0
投票

前端部分是正确的。看起来你的后端没有返回创建的用户。

我建议你在开发工具中检查一下网络活动,当请求到 API_CREATE_USER_URL 已发送。

© www.soinside.com 2019 - 2024. All rights reserved.