Reputation: 301
I am trying to make a function return a string but all it does is return an object. I can't use the .toString()
method either.
currentEnvironment: string = "beta";
serverURL: string = this.setServerURL(this.currentEnvironment);
URL: string = this.serverURL;
async setServerURL(env: string): Promise<string> {
const myText: string = 'https://abcqwer.com';
return myText;
}
async login(): Promise<void> {
console.log('Login URL is: ' + this.URL.toString());
await browser.get(this.URL);
};
I'm getting this error:
TypeError: Parameter "url" must be a string, not object
Upvotes: 0
Views: 895
Reputation: 1643
this method this.setServerURL(this.currentEnvironment)
returns Promise<string>
instead string
. But why you need setServerURL()
to be async
? If you don't do any promise interaction you can re-write it:
setServerURL(env: string): string {
const myText: string = 'https://abcqwer.com';
return myText;
}
Imagine that you need do some promise
things and your setServerURL()
returns Promise<string>
:
currentEnvironment = "beta"; // here typescript understand that variable has string type
serverURL: Promise<string> = this.setServerURL(this.currentEnvironment);
URL: Promise<string> = this.serverURL;
async setServerURL(env: string): Promise<string> {
const myText: string = 'https://abcqwer.com';
return myText; // even if 'myText' is string this method will return Promise<string> because it method has async keyword
}
async login(): Promise<void> {
const url = await this.URL;
console.log('Login URL is: ' + url);
await browser.get(url);
};
Upvotes: 1