maranovot
maranovot

Reputation: 445

No requests during tns debug

this is my first app in nativescript and in mobile development environment in general. And I am having some difficulties. What I am trying is to make some dummy http requests with angular http module but for some reason when I debug the app inside chrome no requests seem to be made.

Here is my code:

template:

<Page>
    <StackLayout>
        <Button text="GET" (tap)="get()"></Button>
        <Button text="POST" (tap)="post()"></Button>
    </StackLayout>
</Page>

component:

import { Component } from "@angular/core";
import { AuthService } from "../../shared/auth.service";
import { HttpClient } from "@angular/common/http";

@Component({
    selector: "register",
    moduleId: module.id,
    templateUrl: "./register.component.html"
})
export class RegisterComponent {

    constructor(private auth: AuthService, private http: HttpClient) {}

    get()
    {
        console.log('GET');
        this.http.get('https://httpbin.org/get');
    }

    post()
    {
        console.log('POST');
        this.http.post('https://httpbin.org/post', null);
    }
}

Now when those functions execute there are no logged requests. I am running inside an emulator and I can browse from it just fine so if someone has some ideas on what could be wrong...

Upvotes: 1

Views: 36

Answers (1)

mast3rd3mon
mast3rd3mon

Reputation: 8835

You have to subscribe to the http requests else they are only defined and not called, try adding the following after this.http.get('https://httpbin.org/get') and this.http.post('https://httpbin.org/post', null):

.subscribe(
    res => console.log(res),
    err => console.log(err),
    () => console.log("Done"),
);

Edit

I didn't mention originally, it's actually best to subscribe to the function that returns the http method, instead of subscribing to the call itself. From experience, I have found some unusual behaviour can occur

Upvotes: 1

Related Questions