Make sure to send individual requests in Angular instead of sending them all at once

I am looking to adjust this function so that it sends these two file ids in separate requests:

return this.upload(myForm).pipe(
  take(1),
  switchMap(res => {
    body.user.profilePic = res.data.profilePic;
    body.user.coverPic = res.data.coverPic;

    return this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body);
})
);

Would using flatmap be a good option here?

Answer №1

If you want to make separate requests using one pipe, you can structure it like this:

return this.upload(myForm).pipe(
  take(1),
  switchMap(res => {
    body.user.profilePic = res.data.profilePic;
    body.user.coverPic = res.data.coverPic;

    return [
      this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
      this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
    ]
  }),
  mergeAll(),
);

Answer №2

Choosing the appropriate operator depends on whether you prefer to send the two requests concurrently or sequentially.

If you already have take(1), then you can opt for either switchMap or mergeMap since it will only emit once regardless of the choice.

For parallel request sending:

return this.upload(myForm).pipe(
  take(1),
  switchMap(res => {
    ...
    return forkJoin([
      this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
      this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
    ]);
  }),
);

For sequential request sending:

return this.upload(myForm).pipe(
  take(1),
  switchMap(res => {
    ...
    return concat(
      this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
      this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
    );
  }),
);

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

Displaying multiple lines of text in a MatSnackbar in Angular is possible

For instance: let message: example;let message2 : example3; For Example: alert(message + '\n'+ message2); Is it possible to display the mat snackbar in Angular in a similar way as shown above?                     ...

Compiling Typescript with union types containing singleton types results in errors

I'm having trouble understanding why the code below won't compile: type X = { id: "primary" inverted?: boolean } | { id: "secondary" inverted?: boolean } | { id: "tertiary" inverted?: false } const a = true const x: X = { id: a ? ...

Tips for creating a coverage report using a gulp task in Angular 4

I've experimented with numerous plugins for setting up gulp tasks to create coverage reports in Angular 4, but none of them seem to be effective. The issue lies in the fact that most documentation is aimed at javascript files, whereas I am dealing wit ...

Bluemix is equipped with a robust build pipeline that allows users to easily set their

I'm in the process of deploying an angular 2 app on Bluemix, with the code stored on GitHub. My goal is to have the app deploy automatically whenever I push changes, so I've set up a pipeline for this purpose. Initially, I started with the build ...

Utilize the automatically detected type of an object for utilization in a Generic context in Typescript

The Scenario I am experimenting with the combination of Alpine.js and TypeScript. To achieve this, I am utilizing the community-maintained typings package @types/alpinejs (GitHub) along with the reusable components design pattern outlined here. Here' ...

The absence of @angular/forms/FormsModule is causing issues

I am facing an issue with importing the FormsModule from @angular/[email protected] while working with Angular 2 RC5. It seems like a simple task, but I am encountering difficulties. import { FormsModule } from '@angular/forms'; Surprising ...

Encountering challenges with the search and filtering features

I'm having some trouble with the search and filter features I'm creating. They work fine initially, but once I enter a search query in the input field, the results show up as expected. However, if I delete the query or enter a different one, the ...

Clear all events from an HTML element and its descendants with TypeScript

Each time the page loads, I have HTML from an API that is constantly changing. Is there a way to strip away all events attached to it? The original HTML looks like this: <div id="content"> <h2 onclick="alert('hi');">Test 1< ...

Inject an asynchronous callback into the constructor of a class by leveraging TypeScript and Node with Passport integration

Currently, I am utilizing the passport-http authentication library. The issue at hand is that its documentation makes use of callbacks while my implementation involves TypeScript classes with async/await functionalities, thus causing uncertainty regarding ...

Preventing an ngrx effect from running when the user logs out

One challenge I am facing involves the dispatch of actions for loading data when a user logs in. The action is captured by an effect like this: getData$ = createEffect(() => this.actions$.pipe( ofType(Actions.getData), exhaustMap(() =& ...

"Using Spyon with the returnValue() method will run the original function, but without the expected

Hello fellow developers, I've been immersing myself in unit testing tutorials lately. However, I've encountered a problem while trying to perform unit tests on an HTTP call example. When I test the function inside my component, it returns the ori ...

Building a typeguard in Typescript that handles errors

type VerifiedContext = Required<ApolloContext>; function authenticateUser(context: ApolloContext): context is VerifiedContext { if (!context.user) { throw new AuthenticationError("Login required for this operation"); } return true; } Here ...

Excessive geolocation position responses in Angular 5

I am trying to implement an Angular 5 component that will continuously fetch my current location every 3 seconds if it has changed. Here is a snippet of my code: export class WorkComponent implements OnInit { constructor(private userService: UserService ...

Is it feasible to retrieve ADFS server users through the Auth0 API in an Asp.net C# MVC application?

Currently, I have successfully integrated ADFS user authentication using the Auth0 API in an ASP.Net application. However, I now have a new requirement to retrieve all ADFS users utilizing the Auth0 API. Is it feasible to fetch all ADFS users through the ...

Dealing with Scoping Problems in a Typescript d3 Update Tutorial

I'm facing challenges trying to implement the provided bl.ocks example in Typescript (using Angular). This is my implementation in TypeScript: StackBlitz Could anyone offer insights on what might be causing the issues I'm encountering? My initi ...

Angular 13's ng serve does not support the keyword "id" for schema ID. Instead, it is recommended to use "$id" to define the schema ID

After successfully upgrading my application from Angular 11 to 13, I encountered some issues while trying to transition from version 12 to 13. Whenever I attempt to run ng serve or ng build, I am faced with the following error: An unhandled exception occ ...

React / NextJS: Repeating Audiowave Component

I am currently developing a chat application in NextJS that includes text-to-speech functionality. To visualize the audio playback waveform, I have integrated a third-party library called wavesurfer.js Though the audio implementation is functioning proper ...

Executing Angular within a docker container

I need to set up a Docker container containing the Angular application image to be served internally. This way, users can easily pull the image, run the application, and start developing without hassle. Here is my current Dockerfile: FROM node:20.11.0-alp ...

How to display placeholder text on multiple lines in Angular Material's mat form field

Within a standard mat-form-field, I have a textarea enclosed. However, the placeholder text for this Textarea is quite lengthy, leading to truncation with an ellipsis on mobile devices due to limited space. My goal is to adjust the placeholder text based ...

Update a particular form field value prior to submission

Currently, I am working on a User registration page that includes the functionality for users to upload their own avatar picture. My approach involves uploading the picture, then calling a function on change to convert the picture into a UInt8Array before ...