Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Error Expected 0 arguments, but got 2 in Ionic Framework

The typescript compiler gives me this error

typescript: src/pages/login/login.ts, line: 33 Expected 0 arguments, but got 2. L32: this.auth.loginAPI(this.registerCredentials) L33: .subscribe(response => {

With this code (2 files):

// auth-provider.ts
public loginAPI(credentials: {email:string,password:string}) {
  return this.http.post(this.apiUrl,data);
}

// login.ts
this.auth.loginAPI(this.registerCredentials)
  .subscribe(response => { 
    console.log(response);
  }, err => {
    console.log(err);
  });

The funny thing is that the code works (I edit any file, my ionic serve reloads and the page renders without any problem).

Any idea?

Environment cli packages: (/usr/local/lib/node_modules)

@ionic/cli-utils  : 1.19.0
ionic (Ionic CLI) : 3.19.0

global packages:

cordova (Cordova CLI) : 7.0.1

local packages:

@ionic/app-scripts : 3.1.5
Cordova Platforms  : ios 4.4.0
Ionic Framework    : ionic-angular 3.9.2

System:

Node  : v8.9.3
npm   : 5.5.1
OS    : macOS High Sierra
Xcode : Xcode 9.2 Build version 9C40b
like image 855
ddalcero Avatar asked Dec 15 '17 02:12

ddalcero


2 Answers

The problem is with the loginAPI function definition. Adding the return type to Observable<any> made the compiler happy and solved the issue.

// auth-provider.ts
public loginAPI(credentials: {email:string,password:string}):Observable<any> {
  return this.http.post(this.apiUrl,data);
}

Still I don't really understand very well why, but at least now it compiles without any error.

like image 82
ddalcero Avatar answered Nov 10 '22 19:11

ddalcero


In order to handler this you can create a class as follows,

public class credentials export {
   email : string;
   password: string;
}

and then,

public loginAPI(inCredentials : credentials ) {
  return this.http.post(this.apiUrl,data);
}
like image 1
Sajeetharan Avatar answered Nov 10 '22 20:11

Sajeetharan