Supplied parameters do not match any signature of

2019-07-31 20:19发布

问题:

I am consuming an api to Covalent UI, on user service. Which needs to post some data from an endpoint to the table as illustrated on the example from the GitHub.

Here is the modification I have made to the service.

import { Provider, SkipSelf, Optional, InjectionToken } from '@angular/core';
import { Response, Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';

import { HttpInterceptorService, RESTService } from '@covalent/http';
import { ApiService } from '../../../../services/api.service';
import { AuthService } from '../../../../services/auth.service';

export interface IUser {
  _id: string;
  email:string;
  createdAt: Date;
  profile: {
      name: string;
      gender: string;
      location: String;
      picture: {
          // data: Buffer; 
          contentType: string;
      }
    }
}

export class UserService extends RESTService<IUser> {

  constructor(private _http: HttpInterceptorService, api: string,
              private authService: AuthService, 
              private api2: ApiService,) {
    super(_http, {
      baseUrl: api,
      path: '/dashboard/users',
    });
  }

  staticQuery(): Observable<IUser[]> {
    // return this._http.get('data/users.json')
    // .map((res: Response) => {
    //   return res.json();
    // });
   return this.api2.get('auth/account/users')
    .map((res: Response) => {
      return res.json();
    });
}
}

export const USERS_API: InjectionToken<string> = new InjectionToken<string>('USERS_API');

export function USER_PROVIDER_FACTORY(
    parent: UserService, interceptorHttp: HttpInterceptorService, api: string): UserService {
  return parent || new UserService(interceptorHttp, api);//<---- This is where I get the error mention.
}

export const USER_PROVIDER: Provider = {
  // If there is already a service available, use that. Otherwise, provide a new one.
  provide: UserService,
  deps: [[new Optional(), new SkipSelf(), UserService], HttpInterceptorService, USERS_API],
  useFactory: USER_PROVIDER_FACTORY,
};

JSON api data

[
    {
        "_id": "59d665c3acbde702b47d3987",
        "updatedAt": "2017-10-07T17:23:00.498Z",
        "createdAt": "2017-10-05T17:02:59.526Z",
        "email": "me@mail.com",
        "password": "$2a$05$z1mRUWqqUfM8wKMU/y9/sOLssAKcV7ydxi0XJyTR1d3BI2X7SSsoy",
        "tokens": [],
        "role": "admin",
        "__v": 0,
        "profile": {
            "name": "F.name L.name",
            "gender": "Female",
            "location": "my place",
            "avatar": {
                "contentType": "image/png",
                "data": "iVBORw0KGgoAAAANSUhEUgAAAaYAAAFmCAYAAAAmm....."
            }
        }
    }
]

Am not sure what am doing wrong, I will appreciate your comment for this fix.

I get the error bellow.

users/services/user.service.ts (51,20): Supplied parameters do not match any signature of call target.

From this line of code

回答1:

As @Philipp mentioned in the comments.

The class UserService expects 4 arguments in the constructor, but you are only providing 2 in the USER_PROVIDER_FACTORY function.

Therefore your factory should be defined:

export function USER_PROVIDER_FACTORY(
    parent: UserService, interceptorHttp: HttpInterceptorService, api: string,
    authService: AuthService, api2: ApiService
): UserService {
  return parent || new UserService(interceptorHttp, api, authService, api2)
}