when utilizing the map operator in Rxjs

After diving into rxjs and Angular recently, I attempted to create an API for accessing a web service.

I started by defining the following type:

    export type Banner = {
           targetId: number;
           url: string;
           imageUrl: string;
    }

The service component looks like this:

export class HomeService {

  constructor(private http: HttpClient,
              @Inject(API_CONFIG) private uri: string) { }

  getBanners(): Observable<Banner[]> {
    return this.http.get(this.uri + 'banner')
      .pipe(map((res: { banners: Banner[] }) => res.banners));
  }
}

And here is an example of how the data is received:

{
"banners": [
{
"imageUrl": "http://p1.music.126.net/64Nc0mcg6Sjq56TzCLDpQA==/109951167536067432.jpg",
"targetId": 0,
...
...
]

Upon compiling the code, I encountered the following error message:

Error: src/app/service/home.service.ts:18:13 - error TS2345: Argument of type 'OperatorFunction<{ banners: Banner[]; }, Banner[]>' is not assignable to parameter of type 'OperatorFunction<Object, Banner[]>'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Property 'banners' is missing in type 'Object' but required in type '{ banners: Banner[]; }'.

Versions used: Typescript 4.5.2, Angular 13.1.0

Answer №1

Make sure to define the type of response expected from the HTTP client:

retrieveBanners(): Observable<{ banners: Banner[] }> {
  return this.http.get<{ banners: Banner[] }>(this.uri + 'banner');
}
...

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

Accessing the locally stored data and displaying it in ng-bind

My journey to learn javascript through this project has hit a roadblock. I have stored an exchange rate in local storage: localStorage.gbpUSD = "1.42746"; Now, I want to utilize it instead of the hardcoded exchange rate in the code below... <input t ...

Rotating an object in Three.js: Animate back and forth along two different azimuth angles

My three.js project features a 3D object that is meant to be viewed from the front only, as it appears as a single plane and is transparent from the back... Using orbitControls, I have restricted movement of both azimuth and polar angle... To enhance the ...

Is it possible to utilize an Angular 8 library within an Angular 4 application?

I am currently working on a project that was originally developed in Angular 4. The project is quite large and converting it all to Angular 8 in one go is not feasible. I am planning to tackle this migration in phases. One approach I am considering is conv ...

Locate Checkbox by its Attribute Name and Value

On my webpage, there are multiple groups of checkboxes. In one particular group, I have added a custom "documentcategory" attribute to each checkbox. My goal is to identify all the checkboxes on the page that have the "documentcategory" attribute with a va ...

Creating a semicircle shape using three.js extrusion

I am trying to create half of an extruded circle using this code, but it only draws a cube. How can I modify it to achieve the desired shape? // Drawing half of an extruded circle var rightfootgeo = new THREE.CubeGeometry(2, 2, 2); for(var i = 0; i < ...

Tips on obtaining a Firebase ID

Does anyone have a solution for retrieving the unique id from Firebase? I've attempted using name(), name, key, and key() without any success. Although I can view the data, I'm struggling to find a way to retrieve the id. This information is cru ...

"Enhance your Angular JS experience with dynamic URL parameters and personalized redirection for improved URL accessibility

I'm struggling to make this code function properly: ..... .when('/channel/:id/:slug',{ templateUrl:'views/channel/index.html', controller:'Channel', publicAccess:true, ...

Tips for customizing the appearance of a jQuery sortable target list prior to dropping items

When using jquery sortable, I am able to customize a placeholder inside a connected list specified in the connectWith option to visualize where the content will be dropped. However, I am struggling to find a way to style the actual list that contains the p ...

Dynamic and operational icons accompany the AngularJS search input field

Currently, I am delving into the world of AngularJS and experimenting with a search box. Within this search box, I want to incorporate three icons: a magnifying glass, a cross, and a spinner. For the first scenario, I envision the magnifying glass appearin ...

What is the best way to pass an array through router navigate function?

I've searched for a solution in other questions, but nothing has helped me... My goal is to redirect to a URL like this: this.router.navigateByUrl('/products'); I want to pass an array and retrieve it in the component with the active link ...

Double invocation of ActivatedRoute.params.subscribe method observed

To extract URL parameters, I'm utilizing the ngOnInit() method where I've implemented the following snippet: this.activatedRoute.queryParams.subscribe(params => { console.log(params); // actual implementation here }); Yet, upon initi ...

What is the process for retrieving the date and time a location was added to Google Maps?

My goal is to extract the date and time a location pin was added in Google Maps. For example, if I search for McDonald's in a specific area, I want to retrieve the dates and times of all McDonald's locations in that area. Is there a method to acc ...

disable event listeners on the DOM

I am currently developing a web framework and focusing on integrating XSS prevention measures. I have successfully implemented data escaping for database storage, but now I am faced with the challenge of allowing users to save generated HTML without riskin ...

Setting a default value in a select tag with ngModel in Angular 6

I am currently working with Angular 6 and have the following code snippet: <select class="form-control form-control-sm" #ig="ngModel" [(ngModel)]="assetFormDetails.ig"> <option value="" >S ...

Can you use a lightbox to showcase multiple images while only displaying one image at a time to trigger them?

Currently, I am utilizing Lightbox in order to showcase photos. For example, I have divided the photos into two categories: "work" and "vacation". To implement this, I would follow this code snippet... <a href="images/image-1.jpg" rel="lightbox[work]"& ...

Leveraging LevelGraph with MemDOWN

I've been experimenting with using LevelGraph and MemDOWN together, but I've noticed that my put and get queries are significantly slower compared to using the filesystem directly with LevelUP. It seems like there might be some mistake in my setu ...

The binding in Knockoutjs is working properly, but for some reason the href attribute in the anchor tag is not redirecting to

Here is the HTML code snippet I am working with: <ul class="nav nav-tabs ilia-cat-nav" data-toggle="dropdown" data-bind="foreach : Items" style="margin-top:-30px"> <li role="presentation" data-bind="attr : {'data-id' : ID , 'da ...

After converting my HTML elements to JSX in Next.js, I am experiencing issues with my CSS files not being applied

Hey there, I'm currently working on a website using Next.js. I've converted the HTML elements of a page to JSX elements but for some reason, the CSS of the template isn't showing up. I've double-checked all the paths and even updated th ...

How to Verify Username in Angular2 and Sails Framework

Currently, I am implementing Angular2 on the front end and Sails.js on the back end. When a user registers, it is necessary to validate whether the chosen username already exists in the database. The backend system developed using Sails will respond with J ...

When utilizing the data property within the useQuery() hook, an error may arise stating "Unable to

This problem has me scratching my head. The code snippet below is triggering this error: TypeError: Cannot read properties of undefined (reading 'map') When I use console.log() to check res.data, everything seems normal and there is data present ...