quarks
quarks

Reputation: 35276

String manipulation with Javascript (NodeJS)

I'm trying to remove the first 13 characters of a string with this code:

requestToken = requestToken.substring(13);

However, I'm getting "has no method substring" error with NodeJS, the code above that mostly recommended in the Javascript forums does not work with NodeJS?

Upvotes: 5

Views: 25408

Answers (7)

Anton Stafeyev
Anton Stafeyev

Reputation: 2859

requestToken.toString().slice(13);

or

if(typeof requestToken!="string")
{
   requestToken.toString().slice(13);
}else
{
   requestToken.slice(13);
}

Upvotes: 0

fider
fider

Reputation: 2036

Try to check your object/variable:

console.log( JSON.stringify(yourObject) );

or it's type by

console.log( typeof yourVariable );

Upvotes: 0

Yusuf X
Yusuf X

Reputation: 14633

Coercing it to a string may not solve your problem. console.log(typeof(requestToken)) might give you a clue to what's wrong.

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35011

it seems like requestToken may not be a string.

Try

requestToken = '' + requestToken;

and then requestToken.substring(13);

Upvotes: 8

Elliot Bonneville
Elliot Bonneville

Reputation: 53291

Convert requestToken to a string first:

requestToken = (requestToken+"").slice(13);

Upvotes: 2

Nevir
Nevir

Reputation: 8101

substring (and substr) are definitely functions on the string prototype in node; it sounds like you're not dealing with a string

$ node
> "asdf".substring(0,2)
'as'

Upvotes: 5

Alex Wayne
Alex Wayne

Reputation: 186984

requestToken must not be a string then. It's likely some sort of object, and the string you want is likely returned by a method on, or a property of, that object. Try console.log(requestToken) and see what that really is.

You also want .slice() for removing the front of a string.

And you will likely end up with something like:

myString = requestToken.someProperty.slice(13);

Upvotes: 1

Related Questions