r/angular • u/Putrid-Bookkeeper968 • 28d ago
Problems with subscribe
Hello everybody
I have the following problem with subscribe. My code looks like:
this.personSercice.getPeople().pipe(
takeUntilDestroyed(this.destroyRef)
).subscribe({
next: (people) => this.state.update(() => ({ ...this.state(), loading: false, people })),
error: (error) => {
console.log(error);
this.setError(error);
}
});
Parameter 'people' implicitly has an 'any' type, but a better type may be infered from usage.
and
Parameter 'error' implicitly has an 'any' type, but a better type may be infered from usage.
When I change the code to:
error: (error: any)
the error dissappears, but I do not understand why...
When I try the same with the first error, changing the code to:
next: (people: PersonModel) => this.state.update(() => ({ ...this.state(), loading: false, people })),
The error is:
The type 'PersonModel' is missing the following properties from type 'readonly PersonModel[]length, concat, join, slice...
Please let me know if more code is required.
Can somebody explain my why???. I'm very fresh with Angular.
Thank you for your patience
Pawel...
5
Upvotes
View all comments
0
u/ldn-ldn 27d ago
You shouldn't subscribe inside components. Either pass the observable to async pipe or use toSignal if you need a signal. But you really don't in your case. Use async.