현재 사용자 데이터를 업데이트하려고합니다. 그래서 여기 난 그냥게시물 요청과 관련된 각도 문제
updateProfile(user){
console.log("update profile func", user)
let headers = new Headers();
this.authToken = this.authService.loadToken();
headers.append('Authorization', this.authToken)
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:17696/users/update', user, {headers:headers})
.map(res => res.json())
}
을 다음과 같은 API에 게시 요청을 updateservice.ts을 만든 다음
<form (submit)="onUserUpdateSubmit()"class="form-horizontal" role="form">
<div class="form-group">
<label class="col-lg-3 control-label">Username:</label>
<div class="col-lg-8">
<input class="form-control" [(ngModel)]="username" name="username" type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email:</label>
<div class="col-lg-8">
<input class="form-control" [(ngModel)]="email" name="email" type="text">
</div>
</div>
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="submit">
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
내 HTML 양식을하지 않습니다 그러나 여기 내 브라우저 개발자 콘솔에서 나에게 오류를주는 원인 나는 붙어있어 나는 사용자 이름과 이메일을 사용하여 console.log 내 객체를 시도했지만 사용자 이름은 false이지만 꽤 괜찮습니다. 또한 내가 console.log 내 개체라는 사용자가 있지만 그것은 정의되지 않습니다. 여기 profile.component.ts입니다
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../../services/auth.service'
import { ValidateService } from '../../services/validate.service'
import { FlashMessagesService } from 'angular2-flash-messages'
import { Router } from '@angular/router'
import { UpdateService } from '../../services/update.service'
@Component({
selector: 'app-profile',
templateUrl: './profile.component.html',
styleUrls: ['./profile.component.css']
})
export class ProfileComponent implements OnInit {
user: Object;
userToUpdate: Object;
username: String;
email: String;
avatar: String;
constructor(private authService: AuthService,
private router: Router,
private flashMessage:FlashMessagesService,
private validateService : ValidateService,
private updateService: UpdateService) { }
ngOnInit() {
this.authService.getProfile().subscribe(profile=>{
this.user = profile.user;
},
err=>{
console.log(err);
return false;
});
}
onUserUpdateSubmit(){
const userToUpdate ={
username:this.username,
email:this.email,
}
console.log(userToUpdate)
//required fields
if(!this.validateService.validateUserUpdate(userToUpdate)){
this.flashMessage.show('Please, fill in all fields', {cssClass: 'alert-danger', timeout:3000});
return false;
}
//validate email
if(!this.validateService.validateEmail(userToUpdate.email)){
this.flashMessage.show('Please, enter correct email', {cssClass: 'alert-danger', timeout:3000});
return false;
}
this.updateService.updateProfile(userToUpdate).subscribe(data=>{
console.log("its from update service profilecomponent.ts")
console.log(data)
if(data/*.success*/){ //commented cause response doesnt have property success but still registering user without displaying the flash message
this.flashMessage.show('You successfuly changed your info', {cssClass: 'alert-success', timeout:3000});
this.router.navigate(['/profile'])
}else{
this.flashMessage.show('Something went wrong', {cssClass: 'alert-danger', timeout:3000});
this.router.navigate(['/profile'])
}
});
}
}
난 당신이 같은 이름을 가진 변수의 두 개의 서로 다른 사본이 그 처음의 호환 문제
원인을 무엇 무엇을 볼 수에서
당신이 사용할 보안 옵션을 실행하고 있습니까 서비스 호출에 로컬 userToUpdate을 통과? 크롬은 종종 이것을 필요로합니다. –
글쎄, 가장 큰 문제는'this.userToUpdate'에 대한 값을 설정하지 않는다는 것입니다. 'userToUpdate'를 로그 한 다음 정의되지 않은'this.userToUpdate' (다른 것)를 사용하려고 시도합니다. –
나는 그것이 정의되지 않은 이유를 전혀 모른다. 나는 그것을 기록했고 그것은 정의되지 않았지만 내가 통과 할 때 그것은 정의되지 않았다. Thats weird – tia0717