Iterate and combine a list of HTTP observables using Typescript

I have a collection of properties that need to be included in a larger mergeMap operation.

this.customFeedsService.postNewSocialMediaFeed(this.newFeed)
            .mergeMap( newFeed => this.customFeedsService.postFeedProperties( newFeed.Id, this.feedProps))
            .mergeMap(newProps => this.customFeedsService.preloadSocialMediaFeed(newProps.FeedId))
            .subscribe(data => console.log(data), err => console.log);

Within my service, I iterate through the properties to generate a single Observable that feeds into the mergeMap process.

public postFeedProperties( feedId: number, props: FeedPropertyApi[] ): Observable<any> {
    let observeGroup = new Observable<any>();
    for(let prop of props){
        prop.FeedId = feedId;
        observeGroup.concat(this.apiService.postData(this.feedPropertyApiUrl, prop, true)
            .map(res => res.json()));
    }

    return observeGroup
    // return this.apiService.postData(this.feedPropertyApiUrl, props[0], true)
    //          .map(res => res.json()).concat(this.apiService.postData(this.feedPropertyApiUrl, props[1],true)
    //          .map(res => res.json())).concat(this.apiService.postData(this.feedPropertyApiUrl, props[2], true)
    //          .map(res => res.json()));
}

While the commented code works perfectly fine, I encounter a

Cannot read property 'subscribe' of undefined
error when running the loop. What could possibly be causing this issue within the loop?

Answer №1

Have you experimented with replacing 'of' with 'as' in your for loop statement?

for(let prop as props) {...}

If the issue persists, can you provide additional details on where the code is encountering errors and whether it occurs during the initial iteration?

Answer №2

Shoutout to Eliseo for providing the solution. This is how I integrated the code into my project.

let groupToObserve: Observable<any>[] = [];
    for(let property of properties){
        property.FeedId = feedIdentifier;
        groupToObserve.push(this.apiService.postData(this.feedPropertyApiUrl, 
        property, true)
            .map(response => response.json()));
    }
return Observable.forkJoin(groupToObserve).map(result => result);

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

"Step-by-step guide on implementing a click event within a CellRenderer in Angular's Ag-Grid

paste your code hereI'm currently working on implementing edit and delete buttons within the same column for each row using Angular ag-Grid. To visually represent these buttons, I am utilizing icons. While I have successfully displayed the edit and de ...

Using TypeScript to asynchronously combine multiple Promises with the await keyword

In my code, I have a variable that combines multiple promises using async/await and concatenates them into a single object const traversals = (await traverseSchemas({filename:"my-validation-schema.json"}).concat([ _.zipObject( [&quo ...

What could be causing issues with my unit tests in relation to Angular Material tooltips?

I have a unique and specific issue with the following unit test code. It is very similar to another working file, but I am encountering an error related to mdTooltip from the Angular Material library. Here's the problematic portion of the code: Phant ...

Changes to the value of an Angular FormControl will automatically trigger changes in the entire formgroup

When attempting to validate the uniqueness of a user's email through an API call, I encountered a problem where any changes in form controls would trigger the value change. Below is my code: Errors(){ this.form01.valueChanges .subscribe((changed ...

Resolving the Issue: How to Solve the "Missing Required Request Body" Error in Angular and Spring MVC

I'm encountering an issue with removing a product from the database using Angular on the frontend. The error message I am receiving is: Required request body is missing: public boolean prodcust.controller.DeleteController.deleteProduct(java.lang.Stri ...

What is preventing the getters for form errors from functioning in my Angular Reactive Form component template, while a direct reference does work?

I have a question regarding a small issue with an Angular reactive form in a lazily loaded signup module. The code structure is structured as follows: TS get email() { return this.myForm.get('email'); } // While this code works from within th ...

Error encountered while retrieving data from Firebase and storing it in an array within an IONIC application

I am currently working on a function that retrieves data from Firebase's real-time database and stores it in an array for mapping in React. However, I am encountering a TypeScript error that I'm having trouble resolving. The error message reads ...

Duplicating an Angular 2 reactive form without retaining the original reference

I am facing a challenge with my reactive form where I need to round certain values when the form is submitted, without altering their appearance on the page. To achieve this, I devised a method that generates a new form, rounds the specified values, and t ...

What is the best way to anticipate a formal announcement?

Hey there! I'm trying to set options for my Datatable and add a new field in my objects, but I need to await the completion of these dtOptions. How can I achieve this in the ngOnInit lifecycle hook? export class MyDashboardComponent implements OnInit ...

Restrict the number of rows in a CSS grid

Hello there, I am in the process of creating an image gallery using CSS grid. Specifically, my goal is to initially show just one row of images and then allow users to click a "Show more" button to reveal additional images. You can see my progress here: ...

Having trouble reaching the angular app when using nginx and istio

I am currently working on setting up an Istio service mesh for a project that involves .NET Core services and Angular 6 as the front end. Interestingly, when I deploy the application with built-in Docker applications, everything runs smoothly. For exampl ...

The transformation from className to class attribute does not occur for custom elements in JSX

I recently encountered an issue with one of my React components where the "className" attribute was being converted to "classname" in the resulting HTML, instead of the expected "class" attribute. The component had a custom element definition as follows: ...

Using Axios and Typescript to filter an array object and return only the specified properties

I'm currently working on creating an API to retrieve the ERC20 tokens from my balance. To accomplish this, I am utilizing nextjs and axios with TypeScript. However, I'm encountering an issue where the response from my endpoint is returning exces ...

What is the best way to guarantee that an object contains certain fields using Partial<>?

I am dealing with a function that accepts a parameter as shown below: const handleAccount = ( account: Partial<IAccountDocument>, ... ) => { ... } It is crucial that the interface for IAccountDocument cannot be modified to avoid requiring cer ...

Setting the Formgroup status to Invalid will only occur if there are errors in multiple fields, not just one

I'm having an issue with my form where setting passportrank as error does not change the formgroup status to Invalid. Additionally, when I navigate to the next section, the inline error message of "You are not eligible" also gets cleared. ngOnInit() ...

Guide on inserting Angular 2 attributes within a variable in an Angular 2 TypeScript class

I have a variable 'content' in the 'findCharityHome.ts' typescript class structured like this. let content = ` <b>`+header+`</b> <button style='background-color:#428bca;color:white; ...

Transferring information using TypeScript

My issue arises when transferring data from HTML in the following format Karbohidrat :{{karbohidrat}} <button ion-button (click)="cekHalamanMakanan('karbohidrat')">View carbohydrate foods</button> <br> Then, I retrieve the kar ...

tsc and ts-node are disregarding the noImplicitAny setting

In my NodeJS project, I have @types/node, ts-node, and typescript installed as dev dependencies. In the tsconfig.json file, "noImplicitAny": true is set. There are three scripts in the package.json file: "start": "npm run build &am ...

Develop a cutting-edge Drag and Drop Functionality using the innovative Material CDK

Here is a unique link you can check out: https://stackblitz.com/angular/nabbkobrxrn?file=src/app/cdk-drag-drop-enter-predicate-example.ts. In this example, I have specific goals: I want to be able to select only one number from the "Available numbers" l ...

Prisma: Utilizing the include option will retrieve exclusively the subobject fields

I created a function to filter the table building and optionally pass a Prisma.BuildingInclude object to return subobjects. async describeEntity(filter: Filter, include?: Prisma.BuildingInclude): Promise<CCResponse> { try { const entity = await ...