简体   繁体   English

CRUD:更新方法不起作用 [Angular]

[英]CRUD : update method doesn't work [Angular]

I faced an issue with update method.我遇到了更新方法的问题。 I'm working on a spring boot angular project so update doesnt work on frontend my code looking logic can someone gie me an idea about this issue我正在开发 spring 启动 angular 项目,所以更新在前端不起作用我的代码查找逻辑可以让我知道这个问题

user.service用户服务

 updateProfile(userData: SignUpData, id: string ): Observable<any> {
    return this.http.patch( API_URL + 'update/' + id, userData, httpOptions);
  }

component.ts组件.ts

form: any = {};
  id: string;
  errorMessage = '';
  currentUser: any;

  constructor(private userservice: UserService, private route: ActivatedRoute, private router: Router, private token: TokenStorageService) { }

  ngOnInit() {
    this.currentUser = this.token.getUser();
  }
 onSubmit() {

    const {adresse1, ...rest} = this.form;

    const userData: SignUpData = {...rest, adresses: [adresse1]};

    this.userservice.updateProfile(userData, this.currentUser.id).subscribe(
      data => {
        console.log(data);

      },
      err => {
        this.errorMessage = err.error.message;
      }
    );
  }

Interceptor.ts拦截器.ts

export class AuthInterceptor implements HttpInterceptor {
  constructor(private token: TokenStorageService) { }

  intercept(req: HttpRequest<any>, next: HttpHandler) {
    let authReq = req;
    const token = this.token.getToken();
    if (token != null) {

      authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, 'Bearer ' + token) });


    }
    return next.handle(authReq);
  }
}

export const authInterceptorProviders = [
  { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
];

I try to logout & log in and that's working because first I didn't get user id so I add this.currentUser = this.token.getUser();我尝试注销并登录,这很有效,因为首先我没有获得用户 ID,所以我添加this.currentUser = this.token.getUser(); without refresh authenticate so GET was always returning 401 not found.没有刷新验证,所以 GET 总是返回 401 not found。 I hope this answer can help people that have the same issue And thank you guys for your replies我希望这个答案可以帮助有同样问题的人谢谢你们的回复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM