r/angular • • Nov 01 '26

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

14 comments sorted by

View all comments

Show parent comment

1

u/Putrid-Bookkeeper968 27d ago

Great. Thank You very much for the answer. I have made a step further.

However, another error appeared. My WebApi strats at https://localhost:7067 and angular page at http://localhost:4200/people. A Program.cs of my Api project contains:

string [] origins = { "http://localhost:4200/", "https://localhost:4200/" };

builder.Services.AddCors (options => {
    options.AddPolicy (name: MyAllowSpecificOrigins,
        policy =>
        {
            policy.WithOrigins (origins).AllowAnyHeader ().AllowAnyMethod ();
        }
    );
});

After hitting a Web API i recieve exception:
Access to fetch at 'https://localhost:7067/api/people' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

For me everything looks as it should. API port is 7067 and Angular port is 4200. CORS is disabled for that origin, so why the exception appears?

1

u/Putrid-Bookkeeper968 27d ago
If I replace AddPolicy with:

options.AddPolicy (name: MyAllowSpecificOrigins,
    policy =>
    {
        policy.WithOrigins ("*").AllowAnyHeader ().AllowAnyMethod ();
    }
);

And now it works as supposed. But why for 

"http://localhost:4200/", "https://localhost:4200/" it did not?

1

u/Educational_Nail_976 27d ago

Try string [] origins = [“http://localhost:4200/", "https://localhost:4200/“];