0

I want to update user information using the Http put method but it keeps returning

detail: "Authentication credentials were not provided

Here is my editComponent.ts

export class MyProfileEditComponent implements OnInit {
    store: any = {};
    editProfileForm: FormGroup;
    private formSubmitAttempt: boolean;

    constructor(fb: FormBuilder, public storeSrv: StoreService) {
        this.editProfileForm = fb.group({
            'mobile': [''],
            'address': [''],
            'description': [''],
            'storeUserId': [''],
    });
}

editProfile() {
   this.formSubmitAttempt = true;
   if (this.store.mobile || this.store.address || this.store.description) {
   this.storeSrv.editStore(this.store.mobile, this.store.address, this.store.description, this.store.storeUserId);
   }
 }
}

and my storeService.ts code

@Injectable()
export class StoreService {
 public updateStoreUrl = this.globals.UPDATE_STORE_URL
 store: any = {};
 storeId: any;

 constructor(private http: Http, private router: Router, private globals: Globals) { }

 editStore(mobile: string, address: string, description: string, storeId: any) {
    let tempStore = localStorage.getItem('store');
    if (tempStore) {
      this.store = JSON.parse(tempStore);
      this.storeId = this.store.user;
    }

    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http.put(this.updateStoreUrl + this.storeId + '/', { 
    headers })
       .subscribe(res => {
          let msg = JSON.parse(res['_body'])['message'];
       }, error => {
          let msg = JSON.parse(error._body)['message'];
       })
   };

I wonder where i am getting this wrong as the api endpoint is expecting the storeId in the request...it all looks good in my eyes but then again...Can you help me spot the error or lead me through right path?

update with editComponent.html

<form [formGroup]="editProfileForm" (ngSubmit)="editProfile()" novalidate>
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group has-feedback" style="position: relative;">
                      <label for="storename">Store Name</label>
                      <input type="text" name="storename" class="form-control" placeholder="{{store.name}}" disabled>
                    </div>
                  </div>

                  <div class="col-md-6">
                    <div class="form-group has-feedback" style="position: relative;">
                      <label for="mobilenumber">Mobile Number</label>
                      <input type="text" name="mobilenumber" class="form-control" placeholder="{{store.mobile}}" [(ngModel)]="store.mobile" [formControl]="editProfileForm.controls['mobile']">
                    </div>
                  </div>

                  <div class="col-md-6">
                    <div class="form-group has-feedback" style="position: relative;">
                      <label for="address">Store Address</label>
                      <textarea name="address" id="" cols="30" rows="10" class="form-control" placeholder="{{store.address1}}"></textarea>
                    </div>
                  </div>

                  <div class="col-md-6">
                    <div class="form-group has-feedback" style="position: relative">
                      <label for="description">Store Description</label>
                      <textarea name="description" id="" cols="30" rows="10" class="form-control" placeholder="{{store.description}}"></textarea>
                    </div>
                  </div>

                  <input type="text" name="storeUserId" [(ngModel)]="store.userId" [formControl]="editProfileForm.controls['storeUserId']" value="{{store.user}}" hidden>

                  <div class="btn-container" style="float: right; margin-right: 15px;">
                    <button class="btn btn-accent btn-flat b-r-10">Edit Profile</button>
                  </div>
                </div>
              </form>
8
  • 1
    Where do you supply the credentials? I don't see it anywhere Commented May 8, 2018 at 13:50
  • gimme a minute lemme update @MikeTung Commented May 8, 2018 at 13:54
  • 1
    Why don't you use http interceptors and provide there the authorization credentials? alligator.io/angular/httpclient-interceptors Commented May 8, 2018 at 13:56
  • @ChristianBenseler from the link you sent there are using the HttpClient but i am working with Http, will it be same use case? Commented May 8, 2018 at 13:59
  • 1
    @CE0 you should move to HttpClient because Http is deprecated as of the latest Angular 6 Commented May 8, 2018 at 14:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.