When using TypeScript in combination with Rxjs, the SwitchMap operator and tap function may not function

After calling setUsersData() and addNewUser(), they should return an Observable<void> once their tasks are complete. Surprisingly, despite the code seeming to work correctly, the tap operator's function is not being executed as expected. The same issue persists even if I attempt to move the logic within the subscribe method. Any suggestions on why this might be happening?

this.userDataService.setUsersData().pipe(
  take(1),
  switchMap(() => {
    if (name !== undefined) {
      console.log('New User created');
      return this.userDataService.addNewUser(resData.email, resData.localId, name);
    } else {
      console.log('Name is undefined. Skipping user creation.');
      return of();
    }
  }),
  tap(() => {
    console.log('data setted');
    this.isLoading = false;
    this.router.navigate(['/microblog']);
  })
).subscribe();

I've attempted to experiment with different RxJs operators, but unfortunately, it hasn't yielded any improvements.

Answer №1

After realizing that both my function addNewUser() and a case with the name undefined were returning of(), I learned that it doesn't actually create an Observable. Making a small adjustment, I updated the return statement to of(void 0) and everything started working perfectly.

Answer №2

When the userDataService is called to set user data, it will only take one instance and then switch to a new operation. If there is a name defined, a new user will be created with the given email and localId, otherwise it will skip user creation. After setting the data, the isLoading flag will be set to false and the router will navigate to '/microblog'.

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

Utilizing Angular http.post to retrieve data from Cloud Function via POST request

Trying to send a POST request to a Google Cloud Function from Angular using @angular/common/http. The documentation for Angular http v7 lacks comprehensive examples, with no information on how to include data or objects in the request. Angular code snippe ...

Eslint requires that return values are used effectively

I am seeking a TypeScript or ESLint rule that enforces the usage of return values from functions. For example: const getNum = () => 12 This rule should allow me to call the function and store the returned value like this: const a = getNum() However, i ...

Is it recommended for synchronous code invoked by a Promise's .then method to generate a fresh Promise

I recently wrote some code containing a mix of asynchronous and synchronous functions. Here's an example: function handleAsyncData() { asyncFunction() .then(syncTask) .catch(error); } After some research, I learned that the then method is ...

Selecting radio buttons using Bootstrap and JavaScript

I'm interested in incorporating this bootstrap radio selection feature into my JavaScript code. For example, if option1 is true, I want to execute a specific action... How can I achieve this? <div class="alert alert-info" role="alert"> < ...

Adding a trailing slash to the URL in an Express server causes it to be appended

I've encountered a strange issue with my express server. Whenever I try to use a specific URL route with the word 'bind,' an extra '/' is automatically added to it. This behavior isn't happening with other URLs that I've ...

Exploring the Possibilities of Socket.io Integration with Express 4 Across Multiple Pages: Dive Into Socket.io Sample Code

Just to clarify, I came across a similar question on Stack Overflow before posting this. However, the answer there was not clear to me and my query is slightly different. Thus, I am hoping for a more straightforward explanation. The Express Generator sets ...

Tips for successfully passing a combined string as an argument in Angular 2 function parameters

Below is the code snippet in question: ... <tr ngFor= "let i = index"> <myCustomElement myName="{{'nameEdit'+i}}"> <button <--I keep encountering the "Got interpolation ({{}}) where expression was exp ...

Utilizing separate routers for distinct subdomains within a Node.js application powered by Express

My goal is to implement dynamic routing in my application based on the subdomain present in req.headers.host. I have devised a simplified solution that looks something like this: var express = require('express'); var domain1 = require(& ...

Getting the response from Google Cloud Translate API using Node JS

Recently, I've been experimenting with the Google Cloud Translate API in NodeJS. While I am able to successfully console.log the value returned from a promise, I am facing challenges passing that value into an EJS template file. Whenever I try to disp ...

Looking for assistance with increasing the output size in JavaScript

As a beginner in JavaScript, I managed to create a form with a textarea and a button to display the text from the textarea in a div tag. Additionally, I added two buttons that are meant to increase the size of the text in the div tag when clicked, as wel ...

What other options exist for searching objects of functions?

Can you suggest some good, easy-to-read, scalable, and efficient alternatives for this basic pattern? type Figure = { kind: "square", sideLength: number } | { kind: "rectangle", length: number, width: number } | { kind: "circle", radius: numbe ...

In React JS, the data from my response is not being saved into the variable

My goal is to store the response data in the variable activityType. Within the useEffect function, I am iterating over an API based on the tabs value. The API will return a boolean value of either true or false. While I can successfully log these values ...

Change the local date and time to UTC in the format of yy:mm:dd H:M

I must change the date format from local time to UTC or ISO as yy:mm:dd H:M, or calculate the difference between local date times with 03:30 as yy:mm:dd H:M 2016-10-22T04:30:00.000Z convert this to 2016-10-22T01:00:00.000Z ...

Angular's DomSanitizer not functioning properly with the Transform css property

I have a question regarding my use of DomSanitizer. The background css property seems to be working fine, but the transform property is not displaying any output. I'm unsure where I might be going wrong in my implementation. In my Angular 8 code: i ...

"Failure in bundling with NodeJS using nexe and fusebox

I am attempting to convert a nodejs application into an .exe file. I initially tried using pkg, but encountered errors with half of the node-modules. Now, I am experimenting with a different method. However, when I run the following command: nexe index.js ...

Running NPM module via Cordova

After developing an app using cordova, I encountered a challenge where I needed to incorporate a node module that lacked a client-side equivalent due to file write stream requirements. My solution involved utilizing Cordova hooks by implementing an app_run ...

Implement a CSS style for all upcoming elements

I'm currently developing a Chrome extension tailored for my personal needs. I am looking to implement a CSS rule that will be applied to all elements within a certain class, even if they are dynamically generated after the execution of my extension&ap ...

How to use Angular pipes to format dates as Long Dates in the template

Suppose I have a date input such as 2022-04-02T00:00:00. When I utilize {{data?.dateStarted | date:'MM/dd/YYYY'}}, the result will be 04/02/2022. But how can we transform it into a Long Date format like April 2, 2022? Does anyone have any sugges ...

Retrieving data sent through an AJAX post request

My current project involves making a POST call from a basic HTML page to a Node.js and Express server that will then save the input values to a MongoDB collection. The issue I am facing is that when passing two POST parameters, namely 'name' and ...

Generating a dynamic SQL Insert statement based on an array object

I am currently working on a Typescript project where I am looking to optimize my Insert function by creating one Insert statement for all the elements in an object, rather than generating multiple Inserts for each array item. Here is the code snippet of m ...