When using RXJS, the method BehaviorSubject.next() does not automatically notify subscribers

In my project, I have a service set up like this:

@Injectable({
  providedIn: 'root'
})
export class MyService {
  private mySubject = new BehaviorSubject({});
  public currentData = this.mySubject.asObservable();

  updateData(data: any) {
     this.mySubject.next(data);
  }
}

There is also a component that subscribes to the Observable:

export class MyComponent implements OnInit {
  constructor(private myService: MyService) { }

  ngOnInit() {
    this.myService.currentData.subscribe(data => /* some logic here */);
  }  
}

Additionally, there is another component that calls the updateData method of the service.

export class AnotherComponent {
  constructor(private myService: MyService) { }

  onClick(data) {
    this.myService.updateData(data);
  }
}

Although I expected the .subscribe in MyComponent to be triggered when this.myService.updateData is called in AnotherComponent.onClick, it does not. However, I can confirm that the Observable has a subscriber attached to it, as the code indicates.

Answer №1

an error was occurring within the closure specified in

this.myService.currentData.subscribe(data => /* some logic here */);
. This error was not being caught, causing the closure to fail when called again in future .next() iterations. By addressing the root cause of the issue or implementing a try...catch block, the problem was successfully resolved.

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

Dealing with the 'UNIFIED_TEST_PLATFORM' issue while trying to compile an Ionic android app that utilizes tesseract.js and capacitor core

I recently set up an Ionic Angular project and integrated Capacitor to access native code functionalities. My project utilizes tesseract.js, as well as Capacitor core and camera plugins. However, I encountered an error while building the Android project: ...

Issues with Rxjs pipe and Angular's Http.get() functionality are causing complications

Working with an Angular 4 Component that interacts with a Service to fetch data is a common scenario. Once the data is retrieved, it often needs to be transformed and filtered before being utilized. The prevailing method for this task is through the use of ...

An error is triggered by Angular when attempting to create a new application due to an invalid project name

Attempting to develop an Angular app for my website: ng new jon-sud.io Encountering an error in Angular: The project name "jon-sud.io" is considered invalid. I am aiming to create an Angular app with the folder named jon-sud.io, not jonSudIo. Why does ...

Issue with material datepicker not initializing start value when input field is clicked

Using the Material date picker, I've implemented the date picker feature with the startAt binding to establish a default selected value. Users can open the calendar overlay by clicking on the input field, thanks to the (click) and (focus) event bindin ...

What is the proper way to define the scope for invoking the Google People API using JavaScript?

I am attempting to display a list of directory people from my Google account. export class People { private auth: Auth.OAuth2Client; private initialized: boolean = false; private accessToken: string; constructor(private readonly clientEmail: strin ...

Using Checkbox Selections to Dynamically Calculate Results in Reactive Forms

I'm struggling to figure out how to retrieve the checkbox value in order to calculate both the "amount" and the "total" values. The calculation process is relatively straightforward: if the checkbox is checked, the amount is determined by (quantity * ...

Creating a component with @input for unit tests in Angular can be achieved through the following steps

I'm encountering issues while attempting to create a testing component with an @input. The component utilizes properties from the feedback model, and although I imported them into the test file, errors are being displayed. Can anyone offer assistance? ...

Error message: "Supabase connection is returning an undefined value

I am encountering an issue with my Vercel deployed Remix project that utilizes Supabase on the backend, Postgresql, and Prisma as the ORM. Despite setting up connection pooling and a direct connection to Supabase, I keep receiving the following error whene ...

Angular 5 introduces a bespoke directive that allows for element repetition within the framework

Looking to create a directive that contains an array of elements, and when the directive is bound to any element, that element will repeat the number of times equal to the length of the array. For example, if the array has 3 elements, the element will re ...

Utilize Angular6 and NodeJS to exhibit images stored on a server file system

Successfully uploaded images to a server but struggling to find a tutorial on how to display these images using Angular6 and NodeJS. Any help would be greatly appreciated. Thank you in advance. Edit: After many trials and errors, I was able to retrieve a ...

The RxDB Angular2-cli error message. "Cannot assign a 'Promise<void>' to a 'Promise<any>' parameter."

https://i.sstatic.net/50vu6.png I've been grappling with getting RxDB to function properly in a fresh project I initiated using the Angular CLI. Here's my process: ng new <Projectname> After that, I installed RxDB by running: npm instal ...

Before computing the width, Next.js Swiper slide occupies the entire width

Check out the code snippet below: 'use client'; import { Swiper, SwiperSlide } from 'swiper/react'; import 'swiper/css'; import 'swiper/css/pagination'; export default function Component() { const cards = [{ id: 0 ...

Guide to verifying a value within a JSON object in Ionic 2

Is there a way to check the value of "no_cover" in thumbnail[0] and replace it with asset/sss.jpg in order to display on the listpage? I have attempted to include <img src="{{item.LINKS.thumbnail[0]}}"> in Listpage.html, but it only shows the thumbna ...

What could be causing my TypeScript project to only fail in VScode?

After taking a several-week break from my TypeScript-based open-source project, I have returned to fix a bug. However, when running the project in VScode, it suddenly fails and presents legitimate errors that need fixing. What's puzzling is why these ...

Experiencing difficulties implementing a Sign in with Google feature with .NET Core 2.1 and Angular 2

Currently, my tech stack consists of Angular 2, Net Core 2.1, and Identity. I've been exploring the option of enabling Google authentication, but have encountered some limitations while using client side gapi libraries - particularly when dealing with ...

I'm confused why this particular method within a class is not being inherited by the next class. Rather than seeing the expected extension, I am presented with - [Function (

Working fine with the Person class, the register() function displays the correct return statement when logged in the console. However, upon extending it to the Employee class, instead of the expected return statement, the console logs show [Function (anon ...

Managing clearing values/strings for different input types like text/password upon form submission in Angular2

Here is a code snippet for an HTML form: <div> <form class="form-horizontal" role="form" (ngSubmit)="createUser(Username.value, Password.value)"> <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6"> <input type="text" class=" ...

Encountering issues with importing a module from a .ts file

Although I have experience building reactJS projects in the past, this time I decided to use Node for a specific task that required running a command from the command line. However, I am currently facing difficulties with importing functions from other fil ...

Tips for effectively combining the map and find functions in Typescript

I am attempting to generate an array of strings with a length greater than zero. let sampleArray2:string[] = ["hello","world","angular","typescript"]; let subArray:string[] = sampleArray2 .map(() => sampleArray2 .find(val => val.length & ...

What steps should be followed to implement ng-zorro-antd?

I'm currently in the process of developing an Angular project with ng-zorro. I've followed these steps: npm install --location=global @angular/cli ng new ng-zorro-demo This Angular project includes routing. cd ng-zorro-demo/ ng add ng-zorro-antd ...