0

I want to have 3 fields with editable text, when I use my method getTexts() it works and the text is displayed in the fields. However when I try to update those texts and update the new values, the http request is never made. I don't get any console errors.

This is my service:

import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';
import { URLSearchParams, QueryEncoder } from '@angular/http';

import { Texts } from '../classes/texts';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class TextsService {

    private getTextsUrl = 'myurl';
    private updateTextsUrl = 'myurl';

    constructor (private http: Http){}


    getTexts(): Observable<Texts>{
        return this.http.get(this.getTextsUrl)
        .map(this.handleData)
        .catch(this.handleError);
    }

    private handleData(res: Response){
        let body = res.json();
        return body;
    }

    private handleError (error: Response | any) {
        let errMsg: string;
        if (error instanceof Response) {
          const body = error.json() || '';
          const err = body.error || JSON.stringify(body);
          errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
        } else {
          errMsg = error.message ? error.message : error.toString();
        }
        return Observable.throw(errMsg);
    }

    updateTextos(texts: Texts): Observable<any>{
        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
        let options = new RequestOptions({ headers: headers });
        let body = new URLSearchParams();

        Object.keys(texts).forEach(key => {
            body.set(key, texts[key]);
        })

This is where the code stops and the http post is never executed

    return this.http.post(this.updateTextsUrl, body.toString(), options)
        .map(this.handleDataAdd)
        .catch(this.handleError);
    }
}

This is my component:

import { Component, OnInit } from '@angular/core';
import { TextosService } from '../../../services/textos.service';
import { Textos } from '../../../classes/textos';

@Component({
  selector: 'app-editar-textos',
  templateUrl: './editar-textos.component.html',
  styleUrls: ['./editar-textos.component.css'],
  providers: [TextsService]
})
export class EditTextsComponent implements OnInit {

  public texts: Textos;
  errorMessage: string;

  constructor(private textsService: TextsService) { }

  ngOnInit() {
    this.getTexts();
  }
  onSubmit(){
    this.updateTexts();
  }

  updateTextos(){
    this.textsService.updateTexts(this.texts);
  }

  getTexts(){
    this.textsService.getTexts()
    .subscribe(
      result => {
        this.texts = new Texts(1, result[0].property1, result[0].property2, result[0].property3);
      },
      error => this.errorMessage = error
    )
  }

}
Geneviv
  • 11
  • 1
  • You don't appear to be calling `updateTextos()` anywhere, which is where the `http.post()` is done. Could it be a typo in your service class, `updateText*o*s` should be `updateTexts`? – Daniel B Oct 18 '17 at 17:19
  • And even if you updateTexts() and updateTextos() is actually the same thing, you don't subscribe to the returned Observable, which is what would send the request. – JB Nizet Oct 18 '17 at 17:22
  • 1
    @DanielB when I submit my form I cal onSubmit() which calls updateTexts() (and yes updateTexts() and updateTextos() is the same thing, I had to translate it) – Geneviv Oct 18 '17 at 17:24
  • But you don't subscribe() to the returned observable, so no request is being sent. – JB Nizet Oct 18 '17 at 17:28
  • @JBNizet you were right! I subscribed to it and now it works. Thank you! – Geneviv Oct 18 '17 at 17:28

0 Answers0