Combine two observables that contain different data types

Here are the functions I am working with:

  getNetworkByName(prefix: string): Observable<Network[]> {
return this.http.get<Network[]>(this.Url + '/networks/search?name-prefix=' + prefix)
  .catch(handleError); }

and

getNetwork(id: number): Observable<Network> {
return this.http.get<Network>(this.Url + '/networks/' + id)
  .catch(handleError); }

I am trying to create a new function called

getNetworkByNameAndId(prefix: string | number): observable<network[]>
that combines the results from the previous two functions. I attempted to use the merge operator but encountered issues due to different types.

Is there an efficient way to solve this without directly subscribing to the functions? This needs to be resolved on the frontend. Sometimes my network name can include special characters like "&" and also, internally, a network Id might be represented as just "1". I want the search to be based on both the name and the id. My attempt looked like this:

return this.getNetworkByName(prefix.toString()).pipe(merge(this.getNetwork(parseInt(prefix,10))); 

This resulted in an error stating:

Type 'Observable<Network | Network[]>' is not assignable to type 'Observable<Network[]>'.

Thank you for your help!

Answer №1

Here's a possible solution:

retrieveNetworkByTitleAndIdentifier(title: string, identifier: number): Observable<Network[]> {
    return Observable.forkJoin(
        retrieveNetworkByTitle(title),
        retrieveNetworkByIdentifier(identifier)
    ).map(([resultA, resultB]) => [...resultA, resultB]);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a Lint No Nested Ternary Error while utilizing the ternary operator

Is there a way to prevent the occurrence of the "no nested ternary" error in TypeScript? disablePortal options={ // eslint-disable-next-line no-nested-ternary units=== "mm&quo ...

Can an Angular library be imported without utilizing npm?

We have a request from management to integrate a new Angular application into our existing main app. The goal is to develop and deploy the new app independently, without needing to deploy the main app every time. I considered creating a library project th ...

In Angular Mat Stepper, only allow navigation to active and completed steps

For a project, I created a sample using React.js and Material UI. Here is the link to the project: https://stackblitz.com/edit/dynamic-stepper-react-l2m3mi?file=DynamicStepper.js Now, I am attempting to recreate the same sample using Angular and Material, ...

Converting typescript path aliases into local file paths

Just dipping my toes into typescript and grappling with module resolution. The problem seems straightforward (or so I think), but there's something off about my tsconfig.json. If my folder structure looks like this: + release + definitions + ...

The Formik and React error is indicating that the '{ refetch: any; }' type is absent

When attempting to pass a prop down to my EmailSignupScreen, I encountered an error message. This issue arose while experimenting with Formik and Typescript. "message": "Type '{ refetch: any; }' is missing the following properties from type &apo ...

The Angular array stays undefined when JSON data is being passed

I am facing an issue with my API that provides JSON data related to football matches. Even after passing this data to the frontend (angular), I am encountering a problem where the array remains undefined. JSON Data: "match_id":"194200", "country_id":"41" ...

Incorporating a new function into a TypeScript (JavaScript) class method

Is it possible to add a method to a method within a class? class MyClass { public foo(text: string): string { return text + ' FOO!' } // Looking for a way to dynamically add the method `bar` to `foo`. } const obj = new MyCl ...

Issue with rendering HTML tags when replacing strings within Ionic 2 and Angular 2

I am currently working with an array of content in my JSON that includes URLs as plain text. My goal is to detect these text URLs and convert them into actual clickable links. However, I'm facing an issue where even though the URL is properly replaced ...

Offer Angular module functionality

I am currently working on an Angular 4 module that I need to import into a larger Angular 4 single-page application. The module consists of a single widget-like component that provides access to various functionalities within the module. The page and modu ...

Is it possible to verify if the @Output is correctly wired up within an Angular component?

When working with Angular and TypeScript, it is possible to access the bound @Input values in the ngOnInit method of a component. However, there isn't a straightforward way to check if a particular @Output event binding has been set up on the componen ...

How can I display options in a react autocomplete feature?

Using the autocomplete component from @material-ui/lab/autocomplete, I am trying to retrieve the title_display result in the options field. These results are being fetched from an API using axios. You can view my code here--> https://codesandbox.io/s/r ...

Encountering an issue while trying to upgrade angular from version 8 to version 16. The error message states: "Unable to bind to 'something' as it is not recognized as a property of 'something'."

Currently in the process of upgrading an old Angular 8 project to Angular 16. The update has been completed, however, when compiling the project I am encountering multiple errors related to components not being able to bind to certain properties that are s ...

Potential absence of object.ts(2531)

Currently, I am working on a project using Node.js with Typescript. My task involves finding a specific MongoDB document, updating certain values within it, and then saving the changes made. However, when I try to save the updated document, an error is bei ...

Encountering compilation issues when transitioning from Angular 7 to Angular 8

Upon upgrading my project to Angular 8, an unexpected error occurs during the build process: ERROR in HostResourceLoader: loader(C:/myapp/cli/src/app/pages/user-home/user-home.component.html) returned a Promise i 「wdm」: Failed to compile. Ho ...

Binding an event to an Angular 2 component directly within its selector code

Looking at my Angular 2 component: import { Component, ElementRef, Renderer } from '@angular/core';; @Component({ selector: 'my-button', templateUrl: 'button.html' }) export class ButtonComponent { private text: string ...

The div with an ngIf directive in Angular 2+ only shows up on the page after it has

I'm facing an issue with Google maps integration on the home page. I want to redirect users to another page when they close the info window displayed on the map: // Code snippet for handling Google map info window closure in a @Directive infowindow ...

The Karma testing feature in Angular Quickstart encounters issues right from the start

When attempting to run karma tests after a clean install of the official Angular quickstart on Windows 10, I encountered an issue. Following a series of four commands, here is what happened: C:\projects\temp>git clone https://github.com/angul ...

Using Angular: Binding Angular variables to HTML for display

I have a component with a ts file that contains HTML content as a variable. Let's say para1= <a href="www.google.com">sitename</a> more content I want to bind this paragraph in HTML as if it were actual HTML tags. sitename What is the ...

Instead of displaying the downloadurl, the `[object Object]` is shown

The console is not displaying the downloadurl, instead [object,Object] [screenshot image]1 this.dbs.collection("databases").get().toPromise().then((snapshot) => { snapshot.docs.forEach(doc=>{ let name=doc.data().path; this.down=this. ...

Setting multiple route parameters for a single segment

Let's jump right into the problem at hand. Is there a way to define multiple route parameters for one segment as shown in the route below: The Routes: { path: 'planlist/:departure_:destination/:date', component: ReservationComponen ...