简体   繁体   中英

TypeError: Cannot call method “replace” of undefined

I'm trying to create a custom function for a google docs spreadsheet. I feel like this is a really simple problem, and I've quickly moved out of my depth. Please help me. A point in the right direction would be much appreciated.

The googledocs script editor gives this error:

TypeError: Cannot call method "replace" of undefined. (line 50)

For this code:

  function replaceGender(name, gender, comment) {

      var genderedComment = String();
      var name;
      var gender;
      var comment;

      if(gender == "m")
      {
          genderedComment = ungenderedComment.replace("(name)", name).replace(/\(He\/She\)/g,"He").replace(/\(His\/\Her\)/g,"His").replace(/\(his\/\her\)/g,"his").replace(/\(him\/\her\)/g,"him").replace(/\(he\/\she\)/g,"he");
      }
      else
      {   
          genderedComment = ungenderedComment.replace("(name)", name).replace(/\(He\/She\)/g,"She").replace(/\(His\/\Her\)/g,"Her").replace(/\(his\/\her\)/g,"her").replace(/\(him\/\her\)/g,"her").replace(/\(he\/\she\)/g,"she");
      }

      return genderedComment;
};

I think its easy, but I'm doing something wrong.

I've changed the code and it works now without error, but the last .replace(/\\(he\\/\\she\\)/g,"she"); and .replace(/\\(he\\/\\she\\)/g,"he"); don't replace.?? no idea... thanks again for all your help... as i said im learning a lot.

here is the code now

function replaceGender(name, gender, comment) {

  if(gender == "m")
  {
    comment = comment.replace(/\(name\)/g, name).replace(/\(He\/She\)/g,"He").replace(/\(His\/\Her\)/g,"His").replace(/\(his\/\her\)/g,"his").replace(/\(him\/\her\)/g,"him").replace(/\(he\/\she\)/g,"he");
  }
  else if(gender == "f")
  {   
    comment = comment.replace(/\(name\)/g, name).replace(/\(He\/She\)/g,"She").replace(/\(His\/\Her\)/g,"Her").replace(/\(his\/\her\)/g,"her").replace(/\(him\/\her\)/g,"her").replace(/\(he\/\she\)/g,"she");
  }

  return comment;
};

Several problems actually, aside from the undefined error. You don't want to declare those variables at the top of the function, since what you need is already passed into the function.

function replaceGender(name, gender, comment) {
  var genderedComment;

  if(gender == "m")
  {
  genderedComment = comment.replace("(name)", name).replace(/\(He\/She\)/g,"He").replace(/\(His\/\Her\)/g,"His").replace(/\(his\/\her\)/g,"his").replace(/\(him\/\her\)/g,"him").replace(/\(he\/\she\)/g,"he");
  }
  else
  {   
  genderedComment = comment.replace("(name)", name).replace(/\(He\/She\)/g,"She").replace(/\(His\/\Her\)/g,"Her").replace(/\(his\/\her\)/g,"her").replace(/\(him\/\her\)/g,"her").replace(/\(he\/\she\)/g,"she");
  }

  return genderedComment;
};

Your variable named ungenderedComment is not defined.


You can test to see if a variable is undefined like this:

if (typeof someVariable === 'undefined') {
    alert("variable is undefined");
}

Or like this:

if (! someVariable) {
    alert("variable is either undefined, null, false, zero, or some falsey value");
}

EDIT: as the comments point out, it looks like you're using the wrong variable altogether!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM