0

I have a few problems concerning Http.post and Http.get with Observables. I'm a student and I'm trying to write a simple WebApp and a Server.

I want to post a boolean on my Server if I press a button. The post process does work but everytime I press the button I subscribe another time to the observable. I had the same problem with my http.get method and resolved the problem with this code.

getdata() {   
if(this.subscribtion2 === null){
        this.isValid = false;
      this.subscribtion2 = this.service.get2().subscribe(

         daten => {
            this.jsonobj = daten;
            this.message = 
                [
                this.jsonobj.data.message1,
                ];
            console.log('subscribe')
            ;
            this.myModelneu = this.message[0];     
             },     
         err => this.handleError(err),
         () => console.log('Simple GET completed')
            );

    }else
   {
       this.isValid = true;
       console.log('unsubscribe')
       this.subscribtion2.unsubscribe();
       this.subscribtion2 = null;

   }
}

The get2() method is in a different class.

  get2() {


        return Observable.interval(3000)
          .switchMap(() => this.http.get('http://127.0.0.1:3000/daten/2'))
          .map(res => res.json())

  }

I don't think that is the common way but I can't find another. My http.post look likes this :

post(json: boolean) {
      console.log('post executed');
      console.log(JSON.stringify(json));

     return this.http.post('http://127.0.0.1:3000/login', { json })
     .subscribe();

   } 

I tried to understand the tutorials with Observables but I did not find how to post data on the server without subscribing to the Observable.

Thanks for your help!

1 Answer 1

2

The http calls, specifically get and post return an observable that completes with the result, hence you don't need to unsubscribe. On completion the subscription is terminated.

Sign up to request clarification or add additional context in comments.

6 Comments

That was exactly my thought, but how do you explain. That if I try the getdata() method without the if loop multiple times. I have multiple services that try to get the data everey 3 seconds? I tried it in the browser and it was exactly like that...
Because the interval generates a new call every 3000 ms. Either do: Observable.timeout(3000) (one time delay) or Observable.interval(3000).take(1)
But this wouldn't work. Perhaps my explanation is not good enough. I have an WebApp and a Server. The WebApp runs in the Browser and the Server on VisualCode. On the Server I have changing data which belongs to a car (dummy). I have a flipswitch on the html from the WebApp which should turn on and off the communication between the server and the webapp. If I turn on the flipswitch I subscribe to an observable which receives data from the server every 3 s. If I switch it of the observable should be canceld. Without the if loop I would creat a new observable every time. Is there another way.
So you want an interval trigger which has a switch? Why not create a single interval and filter it? Observable.interval(3000).filter(v => fetchData).switchMap(...) . This way if you turn the fetchData off, the calls will not be dispatched
I'm not really used to work with observables and also a newbie sorry for that.... How is the fetchData connected to the http.get, is the oberservable cancelled if I 'turn off' the 'fetchData' and how do I do it... :)?
|

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.