cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

Javascript method 'startsWith' does not work with Thingworx

amittal-3
13-Aquamarine

Javascript method 'startsWith' does not work with Thingworx

Hello,

I tried to use a standard Javascript method 'startsWith' which is used to determine, if a certain string begins with the characters of a specified string. But I was not able to use the same in one of my scripts. What could be the probable reason for the same?

Thanks in advance

Regards

Aditya

P.S.: Reference to startsWith

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith

My Thingworx version is "ThingWorx 8.1.0-b52"

1 ACCEPTED SOLUTION

Accepted Solutions
pgrodowski
13-Aquamarine
(To:amittal-3)

This error seems to indicate that the Rhino Javascript interpreter does not know about the startWith method / has no implementation for it, while most things in ECMAScript 5 are supported this seems to be one of the exceptions.

 

As a workaround you can define the method yourself.

 

if (typeof String.prototype.startsWith === 'undefined') 
{
String.prototype.startsWith = function(rhs)
{
return this.indexOf(rhs) === 0;
};
}

 

This also makes it reusable for cases where startsWith does exist as it first checks if the method exists in the String prototype.

 

Let me know if you have any questions.

 

Regards,

Pascal

 

View solution in original post

4 REPLIES 4
pgrodowski
13-Aquamarine
(To:amittal-3)

Hello,

Can you provide a small excerpt of the script that shows the variable declaration and the first use of the startsWith method?

Do you receive any return value or nothing at all?

 

It could also be that the logic never reaches the method, I would first try to add a debug method of some sort to ensure it's called if you receive no return value.

 

Regards,

Pascal

amittal-3
13-Aquamarine
(To:pgrodowski)

Hello Pascal,

I have written a very small Service, and I run that service using the "Test" button to test it. Here is my service code

var temp = "HelloWorld";
var result = temp.startsWith("Hello");

The output of this service is set as "Boolean"

The error that I am getting is - 

"TypeError: Cannot find function startsWith in object HelloWorld."

 

Regards

Aditya

pgrodowski
13-Aquamarine
(To:amittal-3)

This error seems to indicate that the Rhino Javascript interpreter does not know about the startWith method / has no implementation for it, while most things in ECMAScript 5 are supported this seems to be one of the exceptions.

 

As a workaround you can define the method yourself.

 

if (typeof String.prototype.startsWith === 'undefined') 
{
String.prototype.startsWith = function(rhs)
{
return this.indexOf(rhs) === 0;
};
}

 

This also makes it reusable for cases where startsWith does exist as it first checks if the method exists in the String prototype.

 

Let me know if you have any questions.

 

Regards,

Pascal

 

just for the record, in this case I will just directly use your inner code (as you can't have Libraries on server side and you will need the above code on any service that you want o use the startsWith service).

 

Then you just need to use:

 

if (stringValue.indexOf(textToStartWith)===0) ....

 

indexOf service returns -1 if the textToStartWith text isn't included on stringValue or the index position where it starts otherwise, then if it's 0 then it starts with the given text.

 

This will make shorter code services on ThingWorx and I think that's at the same readability level as startsWith

Top Tags