1

For my application I calculate some Value in my Java backend. The only thing I want to do is to receive this String in my Angular frontend, however I struggle with the Observable type.

This is my backend REST Call it uses Spring Boot/Jersey annotations:

@GET
@Path("/calculate/{auftragId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String calculateAwWithJSON(@PathParam("auftragId") String auftragId) {
    restLog("receiving put request /put/auftragId ->> " + auftragId);
    for (AuftragREST anAuftragRepoList : auftragRepoList) {
        if (anAuftragRepoList.getId().replace(" ", "").equals(auftragId)) {
            restLog("successfully calculated AW the requested data!");
            return ObjektprotokollCalculator.calculateAW(anAuftragRepoList).toString();
        }
    }
    return "0";
}

Now the only thing I want to do is get that string in my Angular frontend. However I do not understand how this works with Observables, here is what I have:

public getFromHttpClient(url: string, json: any): Observable<string> {
    return this.httpClient.get<string>(`${this.basePath}/${url}/${json.id}`);
}

However when I try to assing this it doesn't work:

let m: string = this.getFromHttpClient("auftrag/calculate", this.myObject).subscribe();

What's the easiest way to do this? What am I doing wrong? I couldn't find a similar example, everyone always just shows how to do it with Arrays and such but none show how it's done for just a single string.

0

2 Answers 2

2

This is the proper syntax to do it:

this.getFromHttpClient("auftrag/calculate", this.myObject).subscribe((response) => let m: string = response;);
Sign up to request clarification or add additional context in comments.

7 Comments

thanks, but still I get this error error TS2322: Type 'Subscription' is not assignable to type 'string'.
@Miger Where did you call the .subscribe method ?
I do this: let m: string = this.myService.getFromHttpClient("auftrag/calculate", this.myObject()).subscribe(response => { return response });
Yes but where is this line called ?
Yea, you made it wrong.. it has to be inside the subscription like this: this.myService.getFromHttpClient("auftrag/calculate", this.myObject()).subscribe(response => { let m: string = response });
|
0

You need to add a function to the subscribe:

let m: string;
this.getFromHttpClient("auftrag/calculate", this.myObject).subscribe((result: string) => {m = result;});

1 Comment

thanks, but still I get this error error TS2322: Type 'Subscription' is not assignable to type 'string'.

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.