1
votes

I am getting an error TypeError: Method RegExp.prototype.test called on incompatible receiver undefined

var err = {message : "Resume"};

const isResumeError = RegExp('Missing Parameter "Resume"').test;

    if (isResumeError(err.message)) {
        console.log("if");
      }
      else {
           console.log("else");
      }

console.log(isResumeError, "isResumeError");
1
so in if statement i need to put isResumeError.bind(err.message) @blhsing ?art
No. Bind the regex object to the test method.blhsing
can you please tell how right now its giving error bind must be called on function @blhsingart
const isResumeError = (regex = RegExp('Missing Parameter "Resume"')).test.bind(regex); Basically the same as the linked answer if you read it.blhsing
Just separate the assignment statement then. It isn't that complex. Read the linked question. It's the same.blhsing

1 Answers

0
votes

You should bind the regex object to the test method so that it becomes this when the method is called later:

var err = {message : "Resume"};
var regex = RegExp('Missing Parameter "Resume"')
const isResumeError = regex.test.bind(regex);

    if (isResumeError(err.message)) {
        console.log("if");
      }
      else {
           console.log("else");
      }

console.log(isResumeError, "isResumeError");