received TypeError when using combineLatest

import { combineLatest } from 'rxjs';

const obs1 = getAndCreateObservableOne();
const obs2 = getAndCreateObservableTwo();

console.log('Initial output', obs1, obs2);

obs1.subscribe(e => console.log('Second output', e));
obs2.subscribe(e => console.log('Third output', e));

const combinedObs = combineLatest(obs1, obs2);

console.log('Fourth output', combinedObs);

combinedObs.subscribe(e => console.log('Fifth output', e)); // Throws TypeError warning, not functioning

I encountered a situation where my code worked fine as described above. However, yesterday after making changes to what I believed was unrelated code, it stopped working.

The Initial output confirms that both variables are indeed of type Observable.

The outputs from Second output and Third output confirm that each observable is emitting at least one value.

The Fourth output shows that combineLatest() is returning an Observable.

The Fifth output never fires, and commenting out that line removes the warning. Whenever I attempt to use combined.subscribe(), it throws a TypeError warning stating:

TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

What possibly could be causing this issue? I have tried every troubleshooting step I could think of without success. If I'm passing two valid Observables to combineLatest() and they emit values, shouldn't it work accordingly?

As requested, below are the sources for the two observables:

import { BehaviorSubject } from 'rxjs';
import { distinctUntilChanged, map } from 'rxjs/operators';


const getAndCreateObservableOne = () => {
  const subject = new BehaviorSubject<string>(null);
  // Subject.next(...) is used elsewhere
  return subject.asObservable().pipe(distinctUntilChanged());
};

const getAndCreateObservableTwo = () => {
  // Where store represents a redux Store
  const storeSubject = new BehaviorSubject(store.getState());
  store.subscribe(() => {
    storeSubject.next(store.getState());
  });
  const stream = storeSubject.asObservable();

  const customObservable = stream.pipe(
    map(state => {
      return formatData(state) || [];
    })
  );

  return customObservable;
};

Update:

This recent development is truly baffling to me and makes no sense at all. I inserted the following line of code within getAndCreateObservableTwo() before returning customObservable, and oddly enough, everything started working perfectly. If I remove that line, the code stops working again. How does this even make sense? I don't save the new observable to a variable or utilize it in any way. It seems that simply combining them allows the chain to function properly.

combineLatest(customObservable, of(true));

Answer №1

Updated:

import { combineLatest } from 'rxjs/observable/combineLatest';

to:

import { combineLatest } from 'rxjs';

This simple change resolved the issue I was encountering:

TypeError: An incorrect object was provided instead of a stream. Make sure to provide an Observable, Promise, Array, or Iterable.

If the correct import is already in place, there may be an unrelated import causing problems within the code you haven't shared. As some users have mentioned, based on the code snippet provided, everything seems to be in order.

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

What is the best way to identify if a variable in typescript is null?

Initially, I will perform an HTTP request to a URL in order to retrieve some data. { "data": { "user": { "name": "john", "other": [{ "a": 1, "b": 3 }] } } } My go ...

Fetching information from a JSON source and storing it within an array of

I am currently facing an issue where I am unable to assign Exercise[] to Exercise. My goal is to retrieve data from a file, create a class object with the JSON values, and then add it to the class array to display as hardcoded JSON data. As someone who i ...

What are the steps to effectively troubleshoot TypeScript code in Visual Studio 2017?

Currently working on an ASP.NET Core project that heavily utilizes TypeScript. Does Visual Studio support debugging TypeScript code? ...

Troubleshooting a MutationObserver issue in Angular Universal

I recently tried integrating Angular Universal with SSR into my current project. Even though I followed the provided tutorial, everything seemed to be working fine until I attempted to run the project. Upon executing npm run dev:ssr, I received a "Compil ...

Searching Local JSON Data in Ionic 4 with a Filter Input Bar

My current challenge involves filtering local JSON data in my Ionic project. Despite referencing other resources, I am unable to filter or display filtered items on the ngx-datatable. I suspect the issue may lie either in the filterItems function implement ...

Ensure that the input focus is consistently set within the ng-template

When I click on a button in my app, an input element becomes visible. Then, when I click on another button, it gets hidden again. I achieved this functionality using a simple ng-template. However, I encountered a problem - I want the input to automatically ...

Locking mat-toolbar and mat-tabs to the top in Angular Material 2

As I work on my website, my goal is to fix the < Mat-Toolbar > at the top of the screen and then directly underneath that, lock the < Mat-Tabs >. The challenge I'm facing is that the position: fixed in CSS is not working as expected. When ...

Make the if statement easier - Angular

Would you like to know a more efficient way to streamline this If statement? The variables are all strings and are reused in both conditions, but the outcome varies depending on whether it returns true or false. if(params.province && !params.str ...

Unable to retrieve values using any = {} in TypeScript Angular 8

import { Component, OnInit } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { enableProdMode } from '@angular/core'; enableProdMode(); @Component({ selector: 'app-home', templat ...

Implementing a unique sorting algorithm for an array of numbers in Angular

I need to organize an array of numbers in descending order with a custom sorting method based on a specified number, all without splitting or filtering the array. I am currently working with Angular 17 and Rxjs 7.8. For instance, if I have this array of n ...

Generate PDF from HTML in Angular 5 after invoking several services

Currently facing an issue with Angular version 5. I have a parent page which is a review page, and 5 component pages as children. Whenever I navigate to the review page, it automatically takes a snapshot by exporting the HTML to PDF. However, the 5 compone ...

The interaction between two sets of conditional properties results in an error where the value of "boolean" cannot be delineated as "false."

In my setup, I have a specific set of types and two groups of conditional properties structured as follows: export interface Props { label: string children?: ReactNode | undefined } export type OpacityConditionalProp = | { hasOpac ...

Unable to bring the styles.scss file from angular-notifier into my project

I am having trouble importing the styles.scss file from angular-notifier into my project. Below is the error message that I encountered: Module build failed (from ./node_modules/sass-loader/dist/cjs.js): SassError: Unable to locate stylesheet for import. ...

Unable to locate necessary assets, Electron encounters a resource deficiency

I'm currently developing a straightforward app using Angular and Electron. My goal is to set up the project as much from scratch as possible for the purpose of learning. Following this article, I have successfully created a basic Angular project that ...

The declaration module in Typescript with and without a body

I am facing an issue with importing a .mdx file. When I include the following in my mdx.d.ts: /// <reference types="@mdx-js/loader" /> import { ComponentType } from "react"; declare module '*.mdx' { const Component: ...

What is the best way to set up TSLint to apply specific rules with one line and different rules with another line

There is a unique method in which I can specify the code to format, such as forcing the else statement to be on the same line as the ending brace of an if statement. "one-line": [ true, "check-open-brace", "check-catch", "check-else", "check-fin ...

Options are not being shown in the Mat-Select

I am currently working on an Angular application and encountering an issue with displaying options. When the page loads, the options are missing, and there is no error being thrown (nothing in the console). I even attempted a simpler version without connec ...

Validation scheme for the <speak> element

When using validators in an angular formarray for input fields, I encountered a challenge with the regex to check the <speak> tag. https://i.sstatic.net/ogFA3.png The content provided was considered valid. https://i.sstatic.net/ar5FJ.png An error ...

Strategies for pausing loader/spinner animation until all HTTP responses are fully processed in Angular

Implementing a loaderInterceptor to handle spinner display and hiding functionality for multiple HTTP calls on a page. Issue: Experiencing flickering behavior in the spinner between consecutive HTTP calls. I need a solution to only display the spinner wh ...

Does the Typescript compiler sometimes skip adding braces?

I am encountering a problem with compiling a specific section of code in my Angular2 project. public reloadRecords() { let step = (this.timeInterval.max - this.timeInterval.min) / this.recordsChartSteps; let data = new Array(this.recordsChartSteps ...