cheese1756
cheese1756

Reputation: 1929

Object type is the name of the String rather than the String itself when working with process.argv in Node

I'm working with Node, and I'm attempting to use a parameter passed in via the command line as a string. My current code is:

if (process.argv.length > 2) {
        var domain = process.argv[DOMAIN_ARGV];
        if (domain != null) {
            checkdomain(domain);
        }

I then use checkDomain(domain) to strip the prefix off of the domain with:

// Strip off http, https, and www from domains
domain = domain.replaceAll("(http://|http://www\\.|https://|https://www\\.|www\\.)", "");

However, domain is not being treated as a String. Rather, domain's type is the value of the String itself. If I pass in example.com as my domain, I get:

user@ubuntu-laptop:~/src$ node lookup.js example.com

/home/user/src/lookup.js:29
    domain = domain.replaceAll(
                          ^
TypeError: Object example.com has no method 'replaceAll'
    at checkdomain (/home/user/src/lookup.js:29:24)
    at lookup (/home/user/src/lookup.js:19:4)
    at Object.<anonymous> (/home/user/src/lookup.js:63:2)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

The type should be a String, not example.com (or whatever other argument has been passed in). Does anyone know of a solution to this problem?

Upvotes: 0

Views: 50

Answers (2)

vkurchatkin
vkurchatkin

Reputation: 13570

Error message doesn't show type, but toString() result, so everything is correct. Strings doesn't have replaceAll method.

Upvotes: 3

Louis
Louis

Reputation: 151380

Don't you want to use the replace method? If I try calling replaceAll on a string, I get the same error you get. It's not a method that string objects have.

Upvotes: 3

Related Questions