简体   繁体   中英

Why do I encounter this error: “time.replaceAll is not a function”

Hello I keep getting a error of "time.replaceAll is not a function" and I cant figure out why since I tried time.replaceAll in a online code editor and it worked.

exports.ForgotPasswordToken = async (req, res) => {
  username = req.body.username;
  const add_minutes = function (dt, minutes) {
    return new Date(dt.getTime() + minutes * 60000);
  };
  const time = add_minutes(new Date(), 10).toString();
  const decimalTime = time.replaceAll(":", "").substr("16", "6");
  const tokenExp = decimalTime;
  const token = Math.random()
    .toString(24)
    .replace(/[^a-z]+/g, "")
    .substr(0, 5);
  hashed_token = bcrypt.genSalt(10, (err, salt) => {
    bcrypt.hash(token, salt, (err, hash) => {
      if (err) {
        console.log(err);
      }
    });
  });

  await User.findOne({ username: username }, (err, obj) => {
    const userEmail = obj.email;
  });

  let newToken = new Token({
    tokenUser: username,
    tokenVal: hashed_token,
    tokenExpiration: tokenExp,
  });

  const resetLink = `https://localhost:5000/password/reset/${token}/${username}`;
  newToken.save();
  passwordResetEmail(username, userEmail, resetLink);
  res.redirect("/login");
};

String.prototype.replaceAll is a very new feature that only recently became available in JS environments. If you want to use it in Node, either upgrade to Node 15 (15.0.0 is the earliest Node version that supports it), or use a regular expression instead:

time.replace(/:/g, '')

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