8

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
2
  • For clarification, I've tried passing two separate parametrs, a Class, even only one parameter to test it, every time I get the same compiler error. Even SublimeText highlights the error: does it means that we should do it in another way? Commented Dec 15, 2017 at 8:37
  • The problem reported by the compiler is with the .subscribe, not with the credentials passed to loginAPI. I'm editing the question to clarify it better. Commented Dec 15, 2017 at 9:21

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

have you find the reason why this is happening?
@davecar21 seems to me that the compiler is forcing the declaration of the return type...
Is there any other way rather than using Observable<any>
@davecar21 depends on the return type of your function. What are you trying to accomplish or solve?
1

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);
}

4 Comments

Tried with a Class, with an array, with two separate parameters... Doesn't work, same error every time.
what do you mean by with an array ?
I'm calling array the object I pass in my first post ( {email:string,password:string} )
I know, but doesn't work either way. Even with your proposed solution I've got the same error

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.