Reza
Reza

Reputation: 880

property does not exists on type Object from subscribe

Im using a forkJoin to do multiple http calls but it give me the error error TS2339: Property 'data' does not exist on type 'Object'

forkJoin(this.userservice.getUser(), this.userservice.getDashboard()).pipe(
    map(([userData, dashboardData]) => {

      // set the user
      this.user = userData;

      // set order count
      this.orderCount.new = dashboardData.data.new.length;
      console.log(dashboardData);

      this.dataLoaded = true;
    })
).subscribe();

I understand the error because this property comes from a external api so in angular/ionic it is not set. but when i set for example

map(([userData, dashboardData<any>]) => {

or something like that, it does not work. How can i fix this?

the getUser en getDashboard return http objects

    getUser() {
    return this.http.get(environment.baseUrl + '/auth/user').pipe(
        map(results => {
            console.log(results);
            return results;
        })
    );
}

Upvotes: 3

Views: 2141

Answers (2)

bryan60
bryan60

Reputation: 29325

you can type the array like this:

map(([userData, dashboardData]: [UserData, DashboardData]) =>

or you can just type your observables. don't abuse any.

Upvotes: 2

alt255
alt255

Reputation: 3566

In your code, replace this line

  this.orderCount.new = dashboardData.data.new.length;

with this

  this.orderCount.new = (dashboardData as any).data.new.length;

What this line does is cast the Object into typescript's any type.

A better way would be to create model classes for data and uses those model class instead of any.

Upvotes: 2

Related Questions