# RxJS: TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.

**Code:** `You provided 'undefined' where a stream was expected` · **Category:** Angular · **Seen on:** Angular, TypeScript

An RxJS operator (`switchMap`, `mergeMap`, `forkJoin`, `subscribe`) received `undefined` — a function forgot to `return` an Observable.

## Meaning

Operators like `switchMap(x => this.load(x))` must return an Observable. If the inner function has curly braces without a `return`, or a service method is undefined (typo, injection missing), the operator gets `undefined`. Mocks in tests that return nothing cause the same error.

## Common causes

- Arrow function with `{ }` and no `return` inside `switchMap` / `mergeMap`
- Service method or property is `undefined` (typo, wrong injection)
- Test mock returns `undefined` instead of `of(...)`
- `forkJoin` array containing an undefined entry
- Conditional branch returning nothing

## Quick fix

1. Return an Observable from every branch (`return of(null)`)
2. Use an arrow expression without braces
3. In tests mock with `of(value)` or `jasmine.createSpy().and.returnValue(of(...))`

## Detailed fix by platform

### Angular

1. `switchMap(id => this.api.load(id))` (no braces) or `switchMap(id => { return this.api.load(id); })`
2. Provide mocks: `{ provide: Api, useValue: { load: () => of(item) } }`

## Code examples

### Always return a stream

```typescript
this.route.paramMap.pipe(
  map(p => p.get('id')),
  switchMap(id => id ? this.api.load(id) : of(null)),   // both branches return an Observable
).subscribe(item => (this.item = item));
```

## How to diagnose

1. **Operator** — Which operator raised it?
2. **Return** — Does the callback return an Observable?
3. **Mock** — Does the test double return one?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/angular/rxjs-undefined-where-stream-expected)
