0

I created a function that checks if the link matches the path. Everything works when I'm running the local server, but on the build process it fails with the error "Cannot read property 'includes' of undefined'. While i'm digging into the error, what's the best way to reformat this function without using includes()?

 matchLink = (link, path) => {

        return path.includes(link)
    };
7
  • The error is about the includes Commented Feb 15, 2019 at 0:24
  • If it works in development but not production, chances are it’s a build problem not a code problem Commented Feb 15, 2019 at 0:26
  • The error is about path, not includes. path is undefined, thus does not have an includes method. Commented Feb 15, 2019 at 0:27
  • I understand :) That's why I'm asking what's the other best way to reformat this function that's efficient Commented Feb 15, 2019 at 0:28
  • when I console.log path it shows the path, the typeof path also confirms that it's a string Commented Feb 15, 2019 at 0:29

2 Answers 2

2

You should be checking if path is undefined and return false if it is.

If you don't want to use includes, indexOf can work as well.

matchLink = (link, path) => {
  return !!path && path.indexOf(link) > -1;
};
Sign up to request clarification or add additional context in comments.

Comments

1

There are some more alternatives you can use

const matchLink = (link, path) => {
  let reg = new RegExp(link)
  console.log('RegEx Method : ',  reg.test(path))
  console.log('Search Method : ', path.search(link)>-1)
};


matchLink("http://www.example.com","http://www.example.com/123")
matchLink("http://www.example123.com","http://www.example.com/123")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.