argument is not a function. are you looking for mapto() ?
- State: Utah
- Country: United States
- Listed: 5 February 2024 23h16
- Expires: This ad has expired
Description
argument is not a function. are you looking for mapto() ?
# « Argument is not a function. Are you looking for mapTo() ? » : le piège RxJS qui fait rager même les seniors
> TL;DR
> Vous obtenez **« Argument is not a function »** ? 99 % du temps, vous avez oublié de passer une **fonction** à `map` ou vous confondez `map` et l’opérateur **déprécié** `mapTo`. Ce billet vous explique pourquoi, comment corriger, et comment ne plus jamais tomber dans le panneau.
—
## 1. L’erreur qui vous tombe dessus à 2 h du matin
“`ts
this.users$ = this.http
.get(‘/api/users’)
.pipe(
map(this.toViewModel) // 🔥 boom : Argument is not a function
);
“`
Le message semble cryptique, mais il est pourtant limpide :
> « Tu m’as filé *quelque chose* qui n’est **pas une fonction**, alors que je m’attendais à une fonction de projection. »
—
## 2. Deux coupables : `map` VS `mapTo`
| | `map` | `mapTo` (obsolète) |
|—————|———————————–|—————————————-|
| **Signature** | `map(fn: (x) => y)` | `mapTo(constant)` |
| **Rôle** | Transforme **dynamiquement** | Émet **toujours la même valeur** |
| **RxJS 7+** | ✅ à jour | ❌ **supprimé** |
| **Remplacez** | – | `map(() => constant)` |
Exemple :
“`ts
// AVANT RxJS 7
of(1, 2, 3).pipe(mapTo(‘🔔’)); // [‘🔔’,’🔔’,’🔔’]
// APRÈS RxJS 7
of(1, 2, 3).pipe(map(() => ‘🔔’)); // pareil, mais officiel
“`
—
## 3. Les trois scénarios qui tuent votre build
### 3.1 Vous avez oublié le contexte `this`
“`ts
export class UserComponent {
toViewModel(u: User) { /* … */ }
ngOnInit() {
this.users$ = this.http
.get(‘/api/users’)
.pipe(map(this.toViewModel)); // ❌ `this` est `undefined`
}
}
“`
**Fix** :
“`ts
map(u => this.toViewModel(u)) // ✅
// ou
map(this.toViewModel.bind(this)) // ✅
“`
### 3.2 Vous avez mis une valeur au lieu d’une fonction
“`ts
of(1, 2, 3).pipe(map(42)); // ❌ 42 n’est pas une fonction
“`
**Fix** :
“`ts
of(1, 2, 3).pipe(map(() => 42)); // ✅
“`
### 3.3 Vous utilisez encore `mapTo`
“`ts
import { mapTo } from ‘rxjs/operators’; // ❌ n’existe plus
“`
**Fix** :
“`ts
import { map } from ‘rxjs/operators’;
of(…).pipe(map(() => ‘valeur fixe’));
“`
—
## 4. Astuce de pro : vérifier votre version d’RxJS
“`bash
npm ls rxjs
# rxjs@7.8.1 ✅
# rxjs@6.6.7 ⚠️ mapTo existe encore mais est marqué déprécié
# rxjs@9.x ❌ mapTo a disparu
“`
—
## 5. Récap express (copier-coller dans votre code)
“`ts
import { map } from ‘rxjs/operators’;
this.data$ = this.api
.fetchStuff()
.pipe(
map(raw => this.adapt(raw)) // OK
// map(this.adapt), // KO si `this` est perdu
// map(‘toto’), // KO, ce n’est pas une fonction
// mapTo(‘toto’), // KO, mapTo n’existe plus
);
“`
—
## 6. Conclusion
– **Erreur « Argument is not a function »** = vous avez passé *n’importe quoi* à `map`.
– **map** attend une **fonction** : `x => y`.
– **mapTo** est **mort** : remplacez par `map(() => valeur)`.
Une fois ces trois points intégrés, plus jamais vous ne perdrez 45 minutes sur une faute de frappe à 2 heures du matin. Bon codage !
9 total views, 1 today